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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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