From: Stig Bakken Date: Fri, 13 Apr 2001 22:24:33 +0000 (+0000) Subject: * PEAR: look for destructor methods in parent classes if X-Git-Tag: php-4.0.6RC1~427 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=81e1ef0ad7ac4b09904ec0bc2aae0994cd08cf12;p=php * PEAR: look for destructor methods in parent classes if the current class does not have one --- diff --git a/pear/PEAR.php.in b/pear/PEAR.php.in index 4eeda33209..b9d238259e 100644 --- a/pear/PEAR.php.in +++ b/pear/PEAR.php.in @@ -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(); } } diff --git a/pear/tests/pear1.phpt b/pear/tests/pear1.phpt index 2dc49a656d..07b737f5bc 100644 --- a/pear/tests/pear1.phpt +++ b/pear/tests/pear1.phpt @@ -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