PHP Class Properties change after assigned to variable -


take example. never noticed behaviour this:

class foo {     public $foo = 0;      public function addfoo()     {         $this->foo = $this->foo + 1;          return $this;     } }  $bar = new foo;  $a = $bar; $b = $bar->addfoo(); $c = $bar->addfoo(); 

i assumed after script completed:

$a->foo is: 0

$b->foo is: 1

$c->foo is: 2

but that's not case. get:

$a->foo is: 2

$b->foo is: 2

$c->foo is: 2

i don't understand, why $a->foo 2. it's no pointer , never altered.

maybe can explain or show me link php's documentation, covered.

thank you.

objects passed "by reference"*. if assign object variable or return function, you not creating copy of object. explicitly copy object, need clone it.

* not in & meaning of "reference" though. see http://php.net/manual/en/language.oop5.references.php


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -