NYCPHP Meetup

NYPHP.org

[nycphp-talk] More Objects in Sessions

David Mintz dmintz at panix.com
Fri Sep 26 17:18:00 EDT 2003


Scott,

It's hard for me to guess given my experience level and the amount of info
provided, so I tried to reproduce the problem just for kicks (I need to up
my karmic bank balance here anyway (-: ), and there was no problem. It
occurs to me to suggest that you be sure to pass in your objects by
reference, and also assign them by reference. Here's a snippet by way of
example. (btw I'm told that in PHP 5 objects will be passed by reference
automatically.) HTH.

<?php
class Thing {

	var $attribute;
	function Thing(&$attribute)  {
		$this->attribute =& $attribute;
	}

}

class Attribute {

	var $whatever = "I'm an attribute";

	function wassup() {
		echo "wassup yo?\n<";
	}
}

session_start();

if (isset($_SESSION['thing'])) {
	$thing =& $_SESSION['thing'];
	echo "there's a thing in the session";
} else {
	echo "there's no thing in the session. so let's make one... ";
	$attrib = new Attribute();
	$thing = new Thing($attrib);
	$_SESSION['thing'] =& $thing;
}
?>
<pre><?php print_r($thing); ?>
Testing thing's attribute's wassup method:
<?=$thing->attribute->wassup() ?>
</pre>
<? //session_destroy(); ?>

On Fri, 26 Sep 2003, Scott Mattocks wrote:

> Hello,
> Ok, so I have these objects that have other objects as attributes.  I
> can't get the second object to come out of the session properly.  I have
> tried using __sleep and __wakeup to serialize/unserialize but the object
> loses its methods and is pretty much useless.  I have a class method
> that can recreate the second object but it won't recreate it properly if
> called within the class itself.  ex:
> <snip />


---
David Mintz
http://davidmintz.org/
Email: See http://dmintzweb.com/whitelist.php first!

"Y dále p'abajo"

	Tito Rojas



More information about the talk mailing list