1
pwalter
creating custom objects in a xoops session
  • 2006/6/29 3:46

  • pwalter

  • Just popping in

  • Posts: 10

  • Since: 2004/1/26


All,

I *did* search before I posted and couldn't find the answer. If I missed something, I apologize.

I have tried to store / retrieve an custom object in $_SESSION, but on retrieving the object, it does not work properly. Inspecting the object with print_r() shows that it is a "__PHP_Incomplete_Class Object". The class is defined before retrieving the object. Research indicates that to get my code to work properly, the class has to be defined before session_start() is executed in include/common.php, but it is not obvious to me how to do that without modifying either mainfile.php or common.php. Could someone point me to the correct way I should be doing this?

Peter

2
McNaz
Re: creating custom objects in a xoops session
  • 2006/6/29 7:11

  • McNaz

  • Just can't stay away

  • Posts: 574

  • Since: 2003/4/21


Objects can be stored in a session. You have to include/require the object's definition before it is accessed from $_SESSION otherwise you get the "__PHP_Incomplete_Class Object" message.

An Example.

$hUser =& xoops_gethandler('user');
$oUser =$hUser-get(12);
$_SESSION['userObj'] = $oUser;


This will work because the user class has not been defined yet so PHP does not know how to handle the object in the session.

$hUser =& xoops_gethandler('user');
$oUser =& $_SESSION['userObj'];


This will not

$oUser =& $_SESSION['userObj']);


Personally, I use serialize when putting objects in sessions as serialise converts the object into a transportable format.

$_SESSION['userObj'] = serialize($oUser);


HTH.

3
pwalter
Re: creating custom objects in a xoops session
  • 2006/6/29 13:59

  • pwalter

  • Just popping in

  • Posts: 10

  • Since: 2004/1/26


McNaz,

I am not quite sure I completely understood your explanation - but I used serialize() and unserialize() successfully to fix the problem.

Many thanks.

Peter

4
pwalter
Re: creating custom objects in a xoops session
  • 2006/6/30 5:39

  • pwalter

  • Just popping in

  • Posts: 10

  • Since: 2004/1/26


Well, I thought my problem was fixed, but it isn't. My code goes something as follows:
.......
$myobj = '';
if (!empty($_SESSION['mydata']))
{
$serobj = $_SESSION['mydata'];
if(!empty($serobj))
{
$myobj = unserialize($serobj);
}

if (!is_object($myobj))
{
$myobj = '';
unset($_SESSION['mydata']);
}
}
......
......
if (!is_object($myobj))
{
$myobj = new myobjclass($arg);
}
......
if (is_object($myobj))
{
$_SESSION['mydata'] = serialize($myobj);
}
......

The object serializes properly at the bottom of the code. The problem is that the second time I execute the code, I expect $_SESSION['mydata'] to have the serialized object in it at the top of the code block, but it doesn't. What am I doing wrong?

Peter

Login

Who's Online

82 user(s) are online (53 user(s) are browsing Support Forums)


Members: 0


Guests: 82


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Apr 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits