php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #29562 global variable not available when sourced by include()
Submitted: 2004-08-07 20:13 UTC Modified: 2005-02-05 20:52 UTC
Votes:10
Avg. Score:4.8 ± 0.4
Reproduced:9 of 9 (100.0%)
Same Version:3 (33.3%)
Same OS:5 (55.6%)
From: darcy at 1000camels dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.8, 5.0.1 OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: darcy at 1000camels dot com
New email:
PHP Version: OS:

 

 [2004-08-07 20:13 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-08-09 19:05 UTC] 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-09-08 16:21 UTC] 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-09-22 08:02 UTC] okuhl at netcologne dot de
For me, using "$GLOBALS['var']" works fine instead of using global $var.
 [2005-02-05 02:34 UTC] 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();

?>

 [2005-02-05 20:52 UTC] darcy at 1000camels dot com
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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 22 20:00:03 2025 UTC