1

I was reading about php variables and in these two question :

it was mentioned that

When you are coping a variable into another, PHP will copy a reference to the first variable into the second variable. So your new variable, is actually a reference to the first one until now. The value is not copied yet. But if you try to change any of these variables, PHP will make a copy of the value, and then changes the variable.

Does this also apply for SESSION variables? I read on Stack Overflow somewhere that session variables were stored in disk. So until any changes are made to the SESSION variable, the SESSION variable will keep the reference? Is the reverse is also true? i.e.

$var = $_SESSION["var"] or $_SESSION["var"] = $var in both cases will store references?

EDIT:

Also, if I assign a class property in a constructor ($this->var = $var ), will it also hold reference?

Community
  • 1
  • 1
  • It works a lot like Java, which has a better explanation that anything I can find for PHP: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – John V. Apr 28 '17 at 05:45
  • Here is a well respected blog post that seeks to explain in specifically for php: http://blog.golemon.com/2007/01/youre-being-lied-to.html – John V. Apr 28 '17 at 05:49
  • Not exactly what i was asking about, but interesting question, I learned something new. –  Apr 28 '17 at 05:56
  • As far as $_SESSION vars goes, they are treated the same as normal variables during execution. The only special bit is when you start a session it is preloaded from disk, and when the request finishes it is saved to disk (probably, php might do it later depending on which SAPI you are using). – John V. Apr 28 '17 at 06:12

0 Answers0