note 60844 added to language.oop.constructor
| From: | dexen at NOSPAM dot example dot com | Date: | Wed, 18 Jan 2006 10:44:40 +0000 |
| Subject: | note 60844 added to language.oop.constructor | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-102012@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 195.136.184.34Probable Submitter: 213.155.163.9
----
Manual Page -- http://www.php.net/manual/en/language.oop.constructor.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60844
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60844&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60844&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60844&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60844&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60844&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60844&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60844&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60844&report=yes
Search -- http://master.php.net/manage/user-notes.php