hanuman
answered Apr 27 '23 00:00
In PHP, you can store an object in a session by first serializing the object and then storing it in the session.
Here is an example of how to store an object in a PHP session:
session_start();
class Person {
public $name;
public $age;
}
$person = new Person();
$person->name = "John";
$person->age = 30;
$serialized_person = serialize($person);
$_SESSION['person'] = $serialized_person;
$serialized_person = $_SESSION['person'];
$person = unserialize($serialized_person);
echo $person->name;
echo $person->age;
In this example, we create a Person object, serialize it, store it in the session under the person key, retrieve it from the session, unserialize it, and access its properties.
Note that the object must be serializable in order to store it in the session. If the object contains resources or references, it may not be serializable and therefore cannot be stored in the session.