note 60844 deleted from language.oop.constructor by danbrown
| From: | danbrown@php.net | Date: | Sat, 07 Nov 2009 18:57:06 +0000 |
| Subject: | note 60844 deleted from language.oop.constructor by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162523@lists.php.net to get a copy of this message | ||
Note Submitter: dexen at NOSPAM dot example dot com
----
Destructors aren't supported in PHP, and the sollution of using
'register_shutdown_function()' isn't really suitable for situations when you wan to
perform destructor-alike finalization at a PARTICULLAR MOMENT, possibly way before exiting the PHP
machine. (or sort of specialized serialization, as it's in my case).
Anyway, the general problem is to PROPAGATE a call through a hierarchy of objects, based on NAME
PATTERN, in right ORDER (most_derived-to-most_base). Do not rely on INCLUSION of any CODE in derived
classes. Do not assume EXISTENCE of any of 'destruction' functions. Do support DEEP
inheritance.
All i did was to create a _FINI_ function in base class, that calls a 'destructor alike'
functions of pattern _%CLASSNAME_. Now, in order to 'destruct' (or serialize) i do
$OBJ->_FINI_(); then i can safely unset ($OBJ), all data is already saved to remote storage.
So, if ExSOMETHING derives from ExSession, which in turn derives from ExObject, i create
_ExSOMETHING_ function in class ExSOMETHING and _ExObject_ in class ExObject. Notice there's no
_ExSession_, yet nothing breaks.
Code:
<?php
class ExObject {
function _FINI_()
{
$p = get_class($this);
while( $p ) {
$px = '_'.$p.'_';
if ( method_exists($this, $px) ) {
$this->$px();
}
$p = get_parent_class( $p );
}
}
}
?>
You may want to create an interface class Destructable, include _FINI_ in it and derive all relevant
classes from it.