|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-12-03 16:52 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 02:00:01 2025 UTC |
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"; } ?>To give you more inputs, if I replace the echo functions by var_dump, I notice that $foo->instance IS A REFERENCE, as you can see in my printout : object(test2)#1 (1) { ["instance"]=> &int(2) } object(test2_child)#2 (1) { ["instance"]=> int(2) } Furthermore, if I replace "$child = new test2_child()" by "$child = new test2()", there is no bug with __autoload so I suppose thet the problem comes from inheritance ... Hope that helps.