If PHP always copies objects by reference, what happens if an object created in a method is assigned to a member variable? -
I am confused about what will happen in this situation:
public function foo () {$ Obj = new \ stdClass (); $ Obj- & gt; Bar = 'fubar'; $ This- & gt; Obj = $ obj; } If $ obj is copied from the reference, then when foo () returns, then $ Obj can be removed and thus $ this-> obj point to an object that no longer exists?
In the method it looks like this:
$ obj - --------. & Gt; - [object] $ this- & gt; Obj - ' When the method return $ obj variable is lost, and $ This-> Obj will still point to the object:
$ this-> obj ------> [Object] [object] The price will only disappear (or the garbage can be collected), once all the references have been removed.
Comments
Post a Comment