Jump to content

[error] user defined obj passed into session


injh85

Recommended Posts

hi there i've 2 classes Person n Member (extends Person).

i tried to pass a Member obj into the session. when i try

to execute methods from the session i got this error

 

Fatal error: Call to a member function getFullname() on a non-object in C:\wamp\www\katong\admin\admin_index.php on line 9

 

sample codes

class Person

{

function getName()

{

return $this->name;

}

}

 

class Member extends Person

{

function Member($a)

{

}

 

function getLogin()

{

return $this->login;

}

}

 

test1.php

$_SESSION["Member"] = new Member($nric);

 

 

test2.php

$_SESSION['Member'] =  serialize($loginPerson);

$loginPerson = unserialize($_SESSION['Member']);

echo $loginPerson->getName();

C:\Users\Corbin>php -r "class Corbin { function Test() { echo 'hi';} }; $c = new Corbin; $l = serialize($c); $d = unserialize($l); $d->Test();"

hi

 

That works, so theoretically this should work:

 

$m = new Member($nirc);
$_SESSION['Member'] = serialize($m);

//and later

$m = unserialize($_SESSION['member']);
$m->getName();

This might help from the manual -

 

If you do turn on session.auto_start then you cannot put objects into your sessions since the class definition has to be loaded before starting the session in order to recreate the objects in your session.

 

 

All registered variables are serialized after the request finishes. Registered variables which are undefined are marked as being not defined. On subsequent accesses, these are not defined by the session module unless the user defines them later.

 

Warning

Some types of data can not be serialized thus stored in sessions. It includes resource variables or objects with circular references (i.e. objects which passes a reference to itself to another object).

 

If you are using objects, the class definition needs to be in program before the session start.

 

Session data is already serialized, so doing it again won't solve the error.

 

Objects with circular references cannot not be serialized and cannot be put into a session.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.