What happens to object references when they are stored in the session container ($_SESSION)?
In this process, when the values are serialized, are the object references converted into copies or do they simply get broken?
session_start();
$testArrayA = [];
$testArrayB = [];
$testArrayA["abc"] = &$testArrayB;
$testArrayB["def"] = "test2";
$_SESSION["myvalue"] = $testArrayA;
Thanks alot in advance
When the session is written, it gets serialized. Serialization does not care about references at all since it just reads the data (if it’s reading from a reference it will resolve the reference’s target).
You can see this behavior by changing the last line to:
That’s what gets stored in the session.
Note that if an object implements
__sleepor theSerializableinterface its serialization behavior is unique. I believe thatPDOoverrides serialization behavior to destroy its database connection reference.