Background
No mechanism for object destructors exist today although PHP has support for registering
functions which should be run on request shutdown.
Need
Having the ability to define destructors for objects can be very useful. Destructors can log
messages for debugging, clean up temporary files and so on.
Overview
The proposed solution is like in most other OO languages. When the last reference to an
object is destroyed the object’s destructor is called before the object is freed from
memory. Due to the nature of PHP such functionality still needs to be evaluated closely.
For example, when fatal errors occur it might not be possible to call object’s destructors
or objects which are in a referential loop which the reference counting mechanism can’t
detect might not have their destructor called.
Functionality
The user will define a special method in his class definition (which doesn’t receive
arguments). This method will be called __destruct().
So it would look something like:
class MyClass
{
function __destruct()
{
… // Run destructor code
}
}
Like constructors, parent destructors will not be called implicitly by the engine. In order
to run a parent destructor, one would have to explicitly call parent::__destruct() in the
destructor body.
Compatibility notes
No compatibility problems as this feature doesn’t exist in previous versions of the
scripting engine.
Dependencies of feature
This feature is dependent on the new object model.