php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14744 destructor emulation problem (references?)
Submitted: 2001-12-28 13:54 UTC Modified: 2002-03-01 21:47 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: cox at idecnet dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.1.0 OS: Linux
Private report: No CVE-ID: None
 [2001-12-28 13:54 UTC] cox at idecnet dot com
I catched a bug in the PEAR object destructor emulation and could isolate the problem in this script.
<?php
$GLOBALS['_call_destruct'] = null;
class foo {
    var $a = "orig value";
    
    function foo() {
        $this->register();
    }
    
    function register() {
        $GLOBALS['_call_destruct'] =& $this;
    }
}updated value: new value
in destructor: orig value

function _destruct() {
    $foo =& $GLOBALS['_call_destruct'];
    echo "in destructor: {$foo->a}\n";
}

register_shutdown_function('_destruct');

$foo = new Foo;
$foo->a = "new value";
echo "updated value: {$foo->a}\n";
?>

This outputs:
updated value: new value
in destructor: orig value

Which means that the value of the "a" property is lost. Really the whole object is equal to the object comming from the constructor instead of the modified one. This doesn't ocurr if you register the object in a method out the constructor.

The correct output of this should be:
updated value: new value
in destructor: new value

-- Tomas V.V.Cox

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-12-28 14:54 UTC] cox at idecnet dot com
The paste seems that wasn't very good, sorry. The correct code:
<?php
$GLOBALS['_call_destruct'] = null;
class foo {
   var $a = "orig value";
   
   function foo() {
       $this->register();
   }
   
   function register() {
       $GLOBALS['_call_destruct'] =& $this;
   }
}

function _destruct() {
   $foo =& $GLOBALS['_call_destruct'];
   echo "in destructor: {$foo->a}\n";
}

register_shutdown_function('_destruct');

$foo = new Foo;
$foo->a = "new value";
echo "updated value: {$foo->a}\n";
?>

 [2002-03-01 21:47 UTC] ssb@php.net
If you want to use destructors, always instantiate the
object with "&new" instead of "new".

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue May 06 12:01:29 2025 UTC