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
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
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
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

Add a Patch

Pull Requests

Add a Pull Request

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: Fri Feb 23 17:01:28 2024 UTC