php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39721 __autoload and inheritance causes data corruption
Submitted: 2006-12-03 16:52 UTC Modified: 2006-12-05 19:05 UTC
From: dlanoire at neuf dot fr Assigned: dmitry (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.2.0 OS: Windows XP SP2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: dlanoire at neuf dot fr
New email:
PHP Version: OS:

 

 [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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-12-03 17:04 UTC] dlanoire at neuf dot fr
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";
} 
?>
 [2006-12-04 17:04 UTC] dlanoire at neuf dot fr
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.
 [2006-12-04 17:28 UTC] dlanoire at neuf dot fr
fix summary
 [2006-12-05 19:05 UTC] dmitry@php.net
Fixed in CVS HEAD and PHP_5_2.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 14:01:29 2024 UTC