When storing your custom object in a session, the object class needs to be declared before
session_start();.
Wrong way
PHP Code:
<?php
session_start();
require_once('MyClass.php');
if (!$_SESSION['myobj']) {
$_SESSION['myobj'] = new MyClass();
}
$obj = $_SESSION['myobj'] ;
?>
When the object is already stored in the session, this will generate the error:
Right way
PHP Code:
<?php
require_once('MyClass.php');
session_start();
if (!$_SESSION['myobj']) {
$_SESSION['myobj'] = new MyClass();
}
$obj = $_SESSION['myobj'] ;
?>
Makes sense, but had me stumped for a bit.