* PEAR: look for destructor methods in parent classes if

the current class does not have one
This commit is contained in:
Stig Bakken 2001-04-13 22:24:33 +00:00
parent fd7ec5e216
commit 81e1ef0ad7
2 changed files with 69 additions and 24 deletions

View File

@ -30,6 +30,16 @@ define('PHP_BINDIR', '@prefix@/bin');
define('PEAR_INSTALL_DIR', '@PEAR_INSTALLDIR@');
define('PEAR_EXTENSION_DIR', '@EXTENSION_DIR@');
if (substr(PHP_OS, 0, 3) == 'WIN') {
define('OS_WINDOWS', true);
define('OS_UNIX', false);
define('PEAR_OS', 'Windows');
} else {
define('OS_WINDOWS', false);
define('OS_UNIX', true);
define('PEAR_OS', 'Unix'); // blatant assumption
}
$_PEAR_default_error_mode = PEAR_ERROR_RETURN;
$_PEAR_default_error_options = E_USER_NOTICE;
$_PEAR_default_error_callback = '';
@ -76,14 +86,21 @@ class PEAR
* $_PEAR_destructor_object_list for destructor emulation if a
* destructor object exists.
*/
function PEAR() {
if (method_exists($this, "_".get_class($this))) {
global $_PEAR_destructor_object_list;
$_PEAR_destructor_object_list[] = &$this;
}
function PEAR()
{
$classname = get_class($this);
if ($this->_debug) {
printf("PEAR constructor called, class=%s\n",
get_class($this));
print "PEAR constructor called, class=$classname\n";
}
while ($classname) {
$destructor = "_$classname";
if (method_exists($this, $destructor)) {
global $_PEAR_destructor_object_list;
$_PEAR_destructor_object_list[] = &$this;
break;
} else {
$classname = get_parent_class($classname);
}
}
}
@ -276,19 +293,28 @@ class PEAR
// {{{ _PEAR_call_destructors()
function _PEAR_call_destructors() {
function _PEAR_call_destructors()
{
global $_PEAR_destructor_object_list;
if (is_array($_PEAR_destructor_object_list) && sizeof($_PEAR_destructor_object_list)) {
reset($_PEAR_destructor_object_list);
while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
$destructor = "_".get_class($objref);
if (method_exists($objref, $destructor)) {
$objref->$destructor();
if (is_array($_PEAR_destructor_object_list) &&
sizeof($_PEAR_destructor_object_list))
{
reset($_PEAR_destructor_object_list);
while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
$classname = get_class($objref);
while ($classname) {
$destructor = "_$classname";
if (method_exists($objref, $destructor)) {
$objref->$destructor();
break;
} else {
$classname = get_parent_class($classname);
}
}
}
}
// Empty the object list to ensure that destructors are
// not called more than once.
$_PEAR_destructor_object_list = array();
// Empty the object list to ensure that destructors are
// not called more than once.
$_PEAR_destructor_object_list = array();
}
}

View File

@ -18,22 +18,41 @@ class TestPEAR extends PEAR {
}
}
print "test class TestPEAR\n";
class Test2 extends PEAR {
function _Test2() {
print "This is the Test2 destructor\n";
$this->_PEAR();
}
}
class Test3 extends Test2 {
}
print "testing plain destructors\n";
$o = new TestPEAR("test1");
$p = new TestPEAR("test2");
var_dump(get_class($o));
var_dump(get_class($p));
print "..\n";
print "testing inherited destructors\n";
$q = new Test3;
print "..\n";
print "script exiting...\n";
print "..\n";
?>
--GET--
--POST--
--EXPECT--
test class TestPEAR
testing plain destructors
PEAR constructor called, class=testpear
PEAR constructor called, class=testpear
string(8) "testpear"
string(8) "testpear"
..
testing inherited destructors
..
script exiting...
..
This is the TestPEAR(test1) destructor
PEAR destructor called, class=testpear
This is the TestPEAR(test2) destructor
PEAR destructor called, class=testpear
This is the Test2 destructor