#39721 [Opn]: __autoload and inheritance causes data corruption

From: Date: Sun, 03 Dec 2006 17:04:26 +0000
Subject: #39721 [Opn]: __autoload and inheritance causes data corruption
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-105827@lists.php.net to get a copy of this message
ID: 39721 User updated by: dlanoire at neuf dot fr Reported By: dlanoire at neuf dot fr Status: Open Bug Type: Class/Object related Operating System: Windows XP SP2 PHP Version: 5.2.0 New Comment: Sorry, I made a mistake with the source code. The good one is : test.php : <?php include_once "autoload.php"; //include_once "test2.php"; //include_once "test2_child.php"; $foo = new test2(); $child = new test2_child(); echo $foo->instance . "<br>"; echo $child->instance . "<br>"; ?> test2.php : <?php class test2 { private static $instances = 0; public $instance; public function __construct() { $this->instance = ++self::$instances; } } // class ?> test2_child.php : <?php include_once "autoload.php"; class test2_child extends test2 { } // class ?> autoload.php : <?php function __autoload($className) { require_once $className . ".php"; } ?> Previous Comments: ------------------------------------------------------------------------ [2006-12-03 16:52:12] dlanoire at neuf dot fr Description: ------------ __autoload() function causes data corruption when using inheritance. In my example, the value of $foo->instance is 2 and SHOULD BE 1 ! In test.php, if I comment the include_once of autoload.php and uncomment the include_file of classes, the value of $foo->instance is correct (=1). Why ? Normally, there is no difference between autoload and explicit class file inclusion ? Reproduce code: --------------- To test the bug, you need four files. test.php : <?php include_once "autoload.php"; //include_once "test2.php"; //include_once "test2_child.php"; $foo = new test_oop(); $child = new test_oop_child(); echo $foo->instance . "<br>"; echo $child->instance . "<br>"; ?> test2.php : <?php class test_oop { private static $instances = 0; public $instance; public function __construct() { $this->instance = ++self::$instances; } } // class ?> test2_child.php : <?php include_once "autoload.php"; class test_oop_child extends test_oop { } // class ?> autoload.php : <?php function __autoload($className) { require_once $className . ".php"; } ?> Expected result: ---------------- $foo->instance = 1 Actual result: -------------- $foo->instance = 2 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=39721&edit=1

« previous php.bugs (#105827) next »