|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36594 __autoload and require_once() variable scoping issues
Submitted: 2006-03-02 20:14 UTC Modified: 2006-03-02 22:12 UTC
From: graced at monroe dot wednet dot edu Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.1.2 OS: XP Pro
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: graced at monroe dot wednet dot edu
New email:
PHP Version: OS:


 [2006-03-02 20:14 UTC] graced at monroe dot wednet dot edu
When __autoload requires or includes a file, any global variables in that file are in the __autoload function's scope instead of the global scope.

This is consistent with the documented behavior for include() ("When a file is included, the code it contains inherits the variable scope of the line on which the include occurs."), but is not very useful for the __autoload case -- if the class being instantiated require()s another file (say, a function library) and the required file depends on certain global variables that it also defines, it will fail messily as the functions that depend on those variables won't see them, as they are in __autoload()'s scope instead of the global scope.

Reproduce code:
function __autoload($class) {
$foo = new myclass();

class myclass { 

$var = 'some value';
function test() {
	if(isset($GLOBALS['var'])) {
		echo 'var is in the global scope';
	} else {
		echo 'var is not in the global scope';

Expected result:
"var is the global scope" should be echoed.

Actual result:
"var is not in the global scope" is echoed.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-02 22:12 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Changing this would be a major BC break so this won't be 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Feb 23 17:01:28 2024 UTC