1

I am having some memory problems with a script that uses objects set up with inherited static variables like this.

class a
{
    public static $a = "a";
}

class b extends a
{
    private $instanceVar = 'hey';
    private $otherVar = 'you';

    public function DoStuff()
    {
        echo self::$a;
    }
}

then code that uses the classes like this

while(condition)
{
    $obj = new b();
    $obj -> DoStuff();
    unset($obj);
}

My question is, will unsetting obj trigger garbage collection and the unsetting of its instance variables since it also holds a reference to the the inherited static variable?

2 Answers 2

2

unset in this code doesn't bring anything.

With and without it the object will be successfully collected when it's possible.

will unsetting obj trigger garbage collection

Not it won't. Garbage collector will be called automatically when it makes sense.

since it also holds a reference to the the inherited static variable

It doesn't. Objects don't hold references to a static properties.

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

Comments

-1

If you care so much about GC and have PHP >= 5.3.0 have a look to gc_collect_cycles and garbage collection in general

Comments

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.