Re: Storing objects in sessions recursively

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dave M G wrote:
> PHP List,
> 
> I have a class called "Session", which creates an object stored in the
> $_SESSION variable. That object in turn holds on to a "User" object
> which keeps track of the activity of the individual user who has logged
> in and is using the web site. Then the $user object in turn holds on to
> an "Article" object, which keeps information about web content that the
> user is editing, previewing, and viewing.
> 
> The problem is that the "article" object keeps getting lost as the user
> clicks from page to page.
> 
> I'm hoping someone can explain to me why this might be happening.
> 
> As far as I can understand it, an object is basically just an array, and

that might be true of php4 but php5 is rather more sophisticated with regard to objects.

> an object that "owns" an object as a member variable is just like
> storing an array in an array. I can't see any reason why there would be
> any limit to how many levels one could go down.
> 
> So I'm assuming there's no restriction on how many levels of objects
> owning objects there can be.

well you can run out of memory - but what you describe should not be a problem...
and it's not recursive either, it's merely some 'nesting' of objects.

are you loading the Session, User and Article classes BEFORE you start the session?

> 
> As for the code itself, I hope I don't strip out any key details, as
> it's all spread out across many classes. But here's what the basics of
> it look like:
> 
> In the User class, I have this method:
> 
> public function setCurrentArticle($currentArticle)
> {
> $this->currentArticle = $currentArticle;
> }
> 
> In the Article class I have this line:
> 
> Session::getSession()->getUser()->setCurrentArticle($this);

seesm to me you might as well not use an instance of the Session
class and just use it statically (i.e. creating the Session object
just seems to be overhead as afar as I can tell) e.g.

class Session
{
	static function getUser()
	{
		if (!$_SESSION['user'] instanceof User)
			throw new Exception('There is no Spoon.');

		return $_SESSION['user'];
	}

	/* bla */
}

Session::getUser()->setCurrentArticle($this);


> 
> In the Session class I have:
> 
> private static $session;
> private $user;
> 
> public function __construct()
> {
> $this->user = new User();
> }
> 
> public function getUser()
> {
> return $this->user;
> }
> 
> public static function getSession()
> {
> if (!isset($_SESSION['session']))
> {

echo "new session object created. (was this expected)";

> $_SESSION['session'] = new Session();
> }

echo "does the session object contain an article? should it? have a look...";
var_dump($_SESSION['session']);

> return $_SESSION['session'];
> }
> 
> I may have missed a detail in trying to keep this brief and readable. If
> so, please let me know and I will try to provide it.
> 
> Thank you for any advice.

echo(), var_dump(), print_r() - keep sticking in them in your code
till you spot when/where the article object goes missing.

> 
> --
> Dave M G
> Ubuntu 6.06 LTS
> Kernel 2.6.17.7
> Pentium D Dual Core Processor
> PHP 5, MySQL 5, Apache 2
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux