Page 1 of 1

classes into session variables

Posted: Sat Nov 01, 2003 10:40 am
by patnet2004
i just now got to class in my php book and because the instance of the class is stored in variables you lose them when you go into a include file so i was wondering if it was a good idea or not to store them in session variables?

Posted: Sat Nov 01, 2003 12:52 pm
by Cruzado_Mainfrm
how do you explain you lose them? i don't think thats true...

anyways, if u want to save an object(class) u use the serialize() function which takes a variable as parameter and returns a serialized string which can be then passed thru URLs or session variables or whatever you may think of...

to unserialize, just use unserialize() and the parameter will be the serialized string...

good luck

Posted: Sat Nov 01, 2003 1:31 pm
by Gen-ik
I think patnet2004 means that this doesn't work.......

INDEX.PHP

Code: Select all

<?
class Test
{
    function DoSomething()
    {
        echo "Something";
    }
}

$xxx = new Test():

require_once("include.php");

?>
INCLUDE.PHP

Code: Select all

<?
    $xxx -> DoSomething();
?>

Posted: Sat Nov 01, 2003 4:47 pm
by McGruff
Gen-ik wrote:I think patnet2004 means that this doesn't work.......

INDEX.PHP

Code: Select all

<?
class Test
{
    function DoSomething()
    {
        echo "Something";
    }
}

$xxx = new Test():

require_once("include.php");

?>
INCLUDE.PHP

Code: Select all

<?
    $xxx -> DoSomething();
?>
Actually that will work just fine. An included file is basically just another block of the current script (except an include initially jumps out of php - hence the need for php tags if you include a script file).

Any vars in the scope of the include line are available in the included script.

Can you give us some more information about your problem patnet2004?

Posted: Sat Nov 01, 2003 5:49 pm
by patnet2004
oh well i may have just been trippin... :oops:
ya, what i was trying to do is:
Index.php

Code: Select all

<?php
require_once("class.php");
$myClass = new myClass;
require_once("use_class.php);
?>
class.php

Code: Select all

<?php
class myClass
&#123;
	function hello()
	&#123;
		echo"hello";
	&#125;
&#125;
?>
use_class.php

Code: Select all

<?php
$myClass->hello();
?>
but i just tested it again and it works :?
i may have called the wrong file in the browser ("use_class.php instead of index.php")
oh well my mistake, it was late :lol:

Posted: Sat Nov 01, 2003 6:15 pm
by patnet2004
i think i found a way of fixing that self inflicted error...
heres what i made:
index.php

Code: Select all

<?php
class index
&#123;
	var $enabled = NULL;
	function index()
	&#123;
		$this->enabled = 1;
	&#125;
&#125;
$index = new index();

require_once("class.php");
$myClass = new myClass;
require_once("useClass.php");
?>
class.php

Code: Select all

<?php
if(!isset($index->enabled))
&#123;
	die("You didn't start at index.php");
&#125;

class myClass
&#123;	
	function hello()
	&#123;
		echo"hello";
	&#125;
&#125;
?>
useClass.php

Code: Select all

<?php
if(!isset($index->enabled))
&#123;
	die("You didn't start at index.php");
&#125;
$myClass->hello();
?>