#29562 [Bgs]: global variable not available when sourced by include()

From: Date: Sat, 05 Feb 2005 19:52:44 +0000
Subject: #29562 [Bgs]: global variable not available when sourced by include()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-73528@lists.php.net to get a copy of this message
ID: 29562 User updated by: darcy at 1000camels dot com Reported By: darcy at 1000camels dot com Status: Bogus Bug Type: Scripting Engine problem Operating System: * PHP Version: 4.3.8, 5.0.1 New Comment: sniper, i appreciate your comment. i guess i don't understand how it's not a problem with the include() function. when you say it's an issue of scope, that to me, means that the include() is importing the file into a different scope. is that the correct way to think about include(). maybe that's all i don't understand - that included code is within a different scope. Previous Comments: ------------------------------------------------------------------------ [2005-02-05 02:34:05] sniper@php.net It has nothing to do with include but just the scope, your scripts are just the same as this: <?php function testfunction() { global $anObject, $aVariable; // this makes it work function somefunction() { global $anObject, $aVariable; print $anObject->somefunction(); print $aVariable; } class AnotherClass { function somefunction() { return "hello\n"; } } $anObject = new AnotherClass(); $aVariable = "a string"; somefunction(); } testfunction(); ?> ------------------------------------------------------------------------ [2004-09-22 08:02:49] okuhl at netcologne dot de For me, using "$GLOBALS['var']" works fine instead of using global $var. ------------------------------------------------------------------------ [2004-09-08 16:21:44] darcy at 1000camels dot com i got around this problem by globally defining all the variables which are to be used with a global scope. so, just before the include statement, setup your global $variable1, $variable2. this seems to work fine in my case - i was working with phpBB2, which i was trying to try into a CMS i'm working on. It worked fine for phpBB2. i suspect that other code might have trouble with this kind of fix. ------------------------------------------------------------------------ [2004-08-09 19:05:59] darcy at 1000camels dot com i did some more experiments and have found a kind of fix. It would appear that if i globalize any of the variables which are losing scope inside of the function which sources the code, the variables keeps scope. this is interesting and may help me do what i need to do, but i still think this is a problem. if you want to see an example of my fix, look for the files called -fixed on my server: http://1000camels.com/phpBug/ ------------------------------------------------------------------------ [2004-08-07 20:13:16] darcy at 1000camels dot com Description: ------------ This is a somewhat complicated bug to demonstrate. i have placed files online so that the behaviour can be demonstrated. Essentially, the bug is that when you include a php script using include() with a relative url (not absolute), global variables used within a function in the sourced code are not available. this seems to occur only when the original include() is within a class or function. if it is in open code, it works find. it's a complex bug and i'm not sure the best way to analyze. Reproduce code: --------------- http://1000camels.com/phpBug/ four files (and their source): test-open-code.php - works test-within-class.php - does not work test-within-function.php - does not work testMod.php - code which is being sourced Expected result: ---------------- i expect the global variable to be available Actual result: -------------- the global variable (in my example $anObject and $aVariable) are not available. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=29562&edit=1

« previous php.bugs (#73528) next »