]> granicus.if.org Git - php/commitdiff
* PEAR: look for destructor methods in parent classes if
authorStig Bakken <ssb@php.net>
Fri, 13 Apr 2001 22:24:33 +0000 (22:24 +0000)
committerStig Bakken <ssb@php.net>
Fri, 13 Apr 2001 22:24:33 +0000 (22:24 +0000)
  the current class does not have one

pear/PEAR.php.in
pear/tests/pear1.phpt

index 4eeda3320969ee15c40a27b9493156076b879da4..b9d238259e00a2ab019a62312e5cbb6d78a21aac 100644 (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();
     }
 }
 
index 2dc49a656d9c14c166f96a6b6d85ec8ce939e2a8..07b737f5bcc3fbe97ce1c9a77782d8dbf4091135 100644 (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