1

I was trying to understand how does PHP handle objects under the hood. so i ran this small test script This basically

  1. creates a new object,
  2. save it to an array,
  3. return the array element -containing the object- to be saved in other variable.
  4. manipulate object and see if both variable and array objects will be same.

class a
{
    public $a=0;
}
class factory
{
    public $repository=[];

    function make($name){
        $this->repository[]= new $name;
        return end($this->repository);
    }
}

$f = new factory;
$a = $f->make('a'); //a Object([a]=>0);

$a->a = 6;
print_r($f->repository);  //a Object([a] => 6)
print_r($a);        //a Object([a] => 6)

$f->repository[0]->a = 9;

print_r($f->repository);  //a Object([a] => 9)
print_r($a);        //a Object([a] => 9) !

$f->repository[0] = null;
print_r($a);        //a Object([a] => 9) still. SO WHERE DOES THE OBJECT LIVES ?

The Result was that both $a and $this->repository[0] kept sync, any change in state of $a is reflected on repository array element and vise versa.

Yet if i unset $this->repository[0] $a is unaffected (although repo array was the source that created the object in first place).

I feal like i'm missing the basics here, so can some one elaborate how does php handle objects in memory, what happens when you pass object to a variable or a function ?

Note:

i'm aware of object Clone/destruction, my question is not a How to clone or copy question, its about How Does object move around in code, where does it lives, and what am i actually assigning when i assign an object to a variable ?

thanks :)

2
  • In setting $f->repository[0] to null, all you've done is unset that particular reference. See this comment Commented Mar 20, 2014 at 2:38
  • You might find Objects and references worth reading. Commented Mar 20, 2014 at 2:55

2 Answers 2

1

$a and this->repository[0] are both pointers to the same object. Changing the contents of the object is visible through both references. But reassigning either variable changes what that variable refers to, but doesn't change the object that it used to point to.

Sign up to request clarification or add additional context in comments.

Comments

0

As Barmar has said, they are pointers to the same object. My understanding is that when all pointers to that object ($a and this->repository[0]) have been destroyed (set to null, go out of scope, explicitly unset), then the object is no longer accessible - the internal object pointer counter will be 0. When php cleanup happens, the object's memory will (hopefully) be freed. However, I am not privy to the internals - this is just from testing I have done - similar to yours and with tracking memory usage.

3 Comments

You can read your observed behaviour in Reference Counting Basics.
Objects will only free their resources and trigger their __destruct method when all references are unsetted. Even when they are in the object... sigh!, example $a=new a;$a->a=new a; you will need to unset both before object is cleared from memory, unset $a alone will not destroy the object. you need to cast same number of unsets as number of new to destory an object :(. for above example you need to do unset($a->a,$a); to see the __destruct method called and memory freed.
Markus - Great reference and confirms potential memory leak issues in the case of recursive array and object references as Zalaboza has mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.