php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #3223 assignment of a returned reference to an object destroys a property
Submitted: 2000-01-16 06:52 UTC Modified: 2000-06-09 05:43 UTC
From: phaethon at phaethon dot ch Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0 Latest CVS (16/01/2000) OS: Linux 2.0
Private report: No CVE-ID: None
 [2000-01-16 06:52 UTC] phaethon at phaethon dot ch
class class1 {
        var $class2;
        var $foo = 'bar';

        function class1 () {
                $this->class2 = new class2 ($this);
        }

        function &get_ref_to_class2 () {
                        return $this->class2;
        }
}

class class2 {
        var $ref_to_class1;

        function class2(&$ref_to_class1) {
                $this->ref_to_class1 = &$ref_to_class1;
        }
}

eval("\$c1 = new class1;");
/* here it is important, that this assignment is *not * done in the main_op_array, but (for example) in an eval op_array or in an op_array of a function, like the following (otherwise, the bug won't appear):

function make_c1 () {
        global $c1;
        $c1 = new class1;
}
make_c1(); 
*/

/* so now, let's test the stuff */
if(isset($c1->class2->ref_to_class1))
        echo "isset(\$c1->class2->ref_to_class1) is TURE\n";
else    echo "isset(\$c1->class2->ref_to_class1) is FALSE\n";
echo "\$c1->class2->ref_to_class1->foo is ";
echo   $c1->class2->ref_to_class1->foo, "\n<hr>";
/* everything should be ok  so far */

$c2 = &$c1->get_ref_to_class2();   /* critical ...
                                      this seems to `destroy' ref_to_class1 */

/* the same tests again */
if(isset($c1->class2->ref_to_class1))
        echo "isset(\$c1->class2->ref_to_class1) is TURE\n";
else    echo "isset(\$c1->class2->ref_to_class1) is FALSE\n";
echo "\$c1->class2->ref_to_class1->foo is ";
echo   $c1->class2->ref_to_class1->foo, "\n<hr>";  /* fails !!!! */

/* it doesn't matter if one accesses ref_to_class1 through $c1 or $c2, it hasn't
   a valid type any more */
if(isset($c2->ref_to_class1))
        echo "isset(\$c2->ref_to_class1) is TURE\n";
else    echo "isset(\$c2->ref_to_class1) is FALSE\n";

/* ref_to_class1 is set, but of unknown type */
echo "gettype(\$c2->ref_to_class1): ", gettype($c2->ref_to_class1), "\n";



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-06-09 05:43 UTC] zeev at cvs dot php dot net
Circular references don't work well in PHP.  Simply don't use them.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Jun 10 21:01:31 2024 UTC