php.net |  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
 [2006-03-02 20:14 UTC] graced at monroe dot wednet dot edu
Description:
------------
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:
---------------
main.php:
<?php
function __autoload($class) {
	require_once('class.php');
}
$foo = new myclass();

class.php:
<?php
require_once('required.php');
class myclass { 
};

required.php:
<?php
$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';
	}
}
test();



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

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

Patches

Pull Requests

History

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

Changing this would be a major BC break so this won't be 
changed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 10 04:01:27 2024 UTC