PHP垃圾在调用返回后收集函数范围的对象吗?

I know that once a script ends, all objects are destroyed and memory is returned. Does this also happen to function-scoped objects once the function ends that aren't accessible anyway?

For instance, I'm worried about memory leaks in my PHPUnit tests, wherein I create a new object for almost every test. Will this eventually overflow my heap if I run enough tests?

public function testMyFunction()
{
    // Arrange
    $myObject = new MyClass();

    // Act
    $return = $myObject->myFunction();

    // Assert
    $this->assertEquals(true, $return);

}

Should I be manually unsetting them for long-running scripts in an "Absterge" section?

public function testMyFunction()
{
    // Arrange
    $myObject = new MyClass();

    // Act
    $return = $myObject->myFunction();

    // Assert
    $this->assertEquals(true, $return);

    // Absterge
    unset($myObject);
}

PHP will garbage collect once all references to an object are gone.

unset is not needed. However, it is possible that you have a circular dependency, in which case it might not get garbage collected.

The only reason for using unset() is if you'd like to free memory before the end of the function. If there's still something else holding a reference to the thing that you're unsetting, unset() only removes the local variable, but not the object itself.

There's a special garbage collection cycle that cleans up as well circular references. You can control this with this php.ini setting:

http://ca2.php.net/manual/en/info.configuration.php#ini.zend.enable-gc

If you are interested in testing when and if your objects get garbage collected, you could add a __destruct method.