#29562 [Opn->Bgs]: global variable not available when sourced by include()
| From: | sniper@php.net | Date: | Sat, 05 Feb 2005 01:34:08 +0000 |
| Subject: | #29562 [Opn->Bgs]: global variable not available when sourced by include() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-73496@lists.php.net to get a copy of this message | ||
ID: 29562
Updated by: sniper@php.net
Reported By: darcy at 1000camels dot com
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: *
PHP Version: 4.3.8, 5.0.1
New Comment:
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();
?>
Previous Comments:
------------------------------------------------------------------------
[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