I want to make a function, modify for instance, for clients where user can modify passed object BUT not override it with another one.
I mean… if any user wants to override the passed object for instance:
$originObject = new StdClass();
after the function execution
I’ll be able to restore the SAME object passed to the function.
function modify(object &$obj): void {
$obj = new StdClass();
}
function back(object &$obj, int $previousObjectId, string $serializedOriginObject): void {
if (spl_object_id($obj) !== $previousObjectId) {
$obj = unserialize($serializedOriginObject);
}
}
$originObject = new StdClass; // ORIGIN OBJECT
$serializedOriginObject = serialize($originObject);
$objectIdBeforeModify = spl_object_id($originObject);
var_dump($objectIdBeforeModify); // in my case object id: "1"
modify($originObject); // PASSES BY REFERENCE AND OVERRIDES IT
$objectIdAfterModify = spl_object_id($originObject);
var_dump($objectIdAfterModify); // in my case object id: "2"
back($originObject, $objectIdBeforeModify, $serializedOriginObject);
$returnedObjectId = spl_object_id($originObject);
var_dump($returnedObjectId); // in my case object id: "1"
My question is: are there any ways to make it without serialization?
To restore an object that was overriden, cuz it was passed by reference in the function.
Григорий is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.