Re: [PHP] Storing objects in sessions recursively

2006-11-27 Thread Stut

Jochem Maas wrote:

heck it's monday what you expect ;-) (I have the same excuse for fridays)


I use the same excuse Tuesday to Thursday, I find that, on balance, it 
works better for me.


-Stut

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



Re: [PHP] Storing objects in sessions recursively

2006-11-27 Thread Jochem Maas
Stut wrote:
> Jochem Maas wrote:
>> this is wrong - the ctor is not called at all when unserializing,
>> check this
>> code snippet:
>>
>> php -r '
>> class Test { function __construct() { echo "foo\n"; } }
>> $t = new Test;
>> $s = serialize($t);
>> unset($t);
>> $u = unserialize($s);
>> '
>>
>> this only outputs 'foo' once.
>>
>> seems like whatever Dave's problem was it's actually down to

"it's" should have been "it's not" :-P

>> missing ctor args.
> 
> Indeed, my bad. When I think about it it actually doesn't make sense for
> it to be called. Seems to me like the OP needs to be using the __wakeup
> magic method to re-initialise his objects.

yeah, that would probably be the way to go - it's hard to tell without
seeing/knowing what the ctor/init routine was doing.

heck it's monday what you expect ;-) (I have the same excuse for fridays)

> 
> -Stut
> 

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



Re: [PHP] Storing objects in sessions recursively

2006-11-27 Thread Stut

Jochem Maas wrote:

this is wrong - the ctor is not called at all when unserializing, check this
code snippet:

php -r '
class Test { function __construct() { echo "foo\n"; } }
$t = new Test;
$s = serialize($t);
unset($t);
$u = unserialize($s);
'

this only outputs 'foo' once.

seems like whatever Dave's problem was it's actually down to
missing ctor args.


Indeed, my bad. When I think about it it actually doesn't make sense for 
it to be called. Seems to me like the OP needs to be using the __wakeup 
magic method to re-initialise his objects.


-Stut

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



Re: [PHP] Storing objects in sessions recursively

2006-11-27 Thread Jochem Maas
Stut wrote:
> Dave M G wrote:
>> The issue turned out to be my Article class took arguments in its
>> constructor, which was unacceptable because the argument could not be
>> serialized when being passed along during the session.
>>
>> By adjusting my object so that the constructor takes no arguments, and
>> then creating a new method on the object to do what the constructor
>> did previously, the problem was solved.
>>
>> That explanation may be confusing, but that's because I don't fully
>> understand serialization myself yet. All I can say for sure is that
>> the problem was definitely a serialization issue.
> 
> When objects are unserialised the constructor will be called without
> arguments because the values passed to the constructor are not

heh Stut,

this is wrong - the ctor is not called at all when unserializing, check this
code snippet:

php -r '
class Test { function __construct() { echo "foo\n"; } }
$t = new Test;
$s = serialize($t);
unset($t);
$u = unserialize($s);
'

this only outputs 'foo' once.

seems like whatever Dave's problem was it's actually down to
missing ctor args.

> automatically stored in the object. You can get around that without
> having to create a second initialisation method by using default values
> for your arguments...
> 
> class Foo
> {
>public __construct($param = null)
>{
>if (!is_null($param))
>{
>// Parameter passed, do something with it
>}
>}
> }
> 
> -Stut
> 

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



Re: [PHP] Storing objects in sessions recursively

2006-11-27 Thread Jochem Maas
Dave M G wrote:
> Vincent, Jochem,
> 
> Thank you for replying.
> 
> The issue turned out to be my Article class took arguments in its
> constructor, which was unacceptable because the argument could not be
> serialized when being passed along during the session.

why would the arguments need to be serialized? unserializing an object is
not the same as constructing a new one.

> 
> By adjusting my object so that the constructor takes no arguments, and
> then creating a new method on the object to do what the constructor did
> previously, the problem was solved.

are the Article arguments used to setup a DB connection (or something like 
that) per chance?

> 
> That explanation may be confusing, but that's because I don't fully
> understand serialization myself yet. All I can say for sure is that the
> problem was definitely a serialization issue.
> 

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



Re: [PHP] Storing objects in sessions recursively

2006-11-27 Thread Stut

Dave M G wrote:
The issue turned out to be my Article class took arguments in its 
constructor, which was unacceptable because the argument could not be 
serialized when being passed along during the session.


By adjusting my object so that the constructor takes no arguments, and 
then creating a new method on the object to do what the constructor 
did previously, the problem was solved.


That explanation may be confusing, but that's because I don't fully 
understand serialization myself yet. All I can say for sure is that 
the problem was definitely a serialization issue.


When objects are unserialised the constructor will be called without 
arguments because the values passed to the constructor are not 
automatically stored in the object. You can get around that without 
having to create a second initialisation method by using default values 
for your arguments...


class Foo
{
   public __construct($param = null)
   {
   if (!is_null($param))
   {
   // Parameter passed, do something with it
   }
   }
}

-Stut

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



Re: [PHP] Storing objects in sessions recursively

2006-11-27 Thread Dave M G

Vincent, Jochem,

Thank you for replying.

The issue turned out to be my Article class took arguments in its 
constructor, which was unacceptable because the argument could not be 
serialized when being passed along during the session.


By adjusting my object so that the constructor takes no arguments, and 
then creating a new method on the object to do what the constructor did 
previously, the problem was solved.


That explanation may be confusing, but that's because I don't fully 
understand serialization myself yet. All I can say for sure is that the 
problem was definitely a serialization issue.


--
Dave M G
Community Moderator

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



RE: [PHP] Storing objects in sessions recursively

2006-11-24 Thread Vincent DUPONT

Hi,

are you sure you did not forget a include of the Article source code ?
You need all sources codes aiavlable (included) when getting the object back 
from the session.

vincent

-Original Message-
From: Dave M G [mailto:[EMAIL PROTECTED]
Sent: Fri 24/11/2006 11:22
To: php-general@lists.php.net
Subject: [PHP] Storing objects in sessions recursively
 
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 
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.

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);

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']))
{
$_SESSION['session'] = new 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.

?--
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

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



Re: [PHP] Storing objects in sessions recursively

2006-11-24 Thread Jochem Maas
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



[PHP] Storing objects in sessions recursively

2006-11-24 Thread Dave M G

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 
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.


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);

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']))
{
$_SESSION['session'] = new 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.

--
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