开发者

Objects inside $_SESSION, undocumented behaviour

开发者 https://www.devze.com 2023-03-10 11:56 出处:网络
Please have look on the following code: $_SESSION[\"process_y\"] = new Process(); $process_y = $_SESSION[开发者_运维问答\"process_y\"];

Please have look on the following code:

$_SESSION["process_y"] = new Process();
$process_y = $_SESSION[开发者_运维问答"process_y"];
$process_y->name = "John";
$process_y = $_SESSION["process_y"];
echo " name is ".$process_y->name;
// Outputs "name is John"

I fetch an object from a SESSION variable. Then I modify a member of this object, but never saved it back to session. For some reason I'm not aware of, the modified object gets saved in the session like if the fetched object would be a "pointer".

I couldn't find any reference explaining this behaviour in php documentation.

NOTE: register_globals is OFF.


That behavior isn't undocumented and it has nothing to do with the global _SESSION-Array. In some way you are right: It's a pointer. Check the manual for full explanation: http://www.php.net/manual/en/language.oop5.references.php


As the $_SESSION variable is global, any mutations to it will be saved. In addition to that the $_SESSION spans over the individual requests (as a session should), so the behaviour is as expected.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号