I am trying to store objects in session
However, when I do the following, I don’t get output as expected
Code:
In the session am storing objects like this –
$oEmployee = new Employee();
$_SESSION['Employee'] = $oEmployee;
And am trying to print like this –
print_r($_SESSION['Employee']);
echo "<br>";
$oEmployee = new $_SESSION['Employee'];
print_r($oEmployee);
This is the output I am getting:
Employee Object
(
[m_sFirstName:private] => Sac
[m_sLastName:private] => Kos
[m_sPhone:private] => 9876543210
[m_sEmail:private] => sac@kos.com
[m_sPassword:private] => password
[m_iEmployeeId:private] => 1
)
Employee Object
(
[m_sFirstName:private] =>
[m_sLastName:private] =>
[m_sPhone:private] =>
[m_sEmail:private] =>
[m_sPassword:private] =>
)
Any idea what’s going wrong ?
The problem lies with your line
PHP casts the value in
$_SESSION['Employee']to string, and then tries to instantiate a class with that name (new). Since you aren’t passing anything to the constructor, an empty object is instantiated.What are you trying to do in that line?