|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26174 Global vs. class vs. variable scope w/ include
Submitted: 2003-11-08 02:30 UTC Modified: 2003-11-08 11:55 UTC
From: myle34 at hotmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.0.0b2 (beta2) OS: Windows XP Home
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
47 - 43 = ?
Subscribe to this entry?

 [2003-11-08 02:30 UTC] myle34 at hotmail dot com
This is related to Bug #26173 ( but the reproduce code was wrong.

I am using Windows XP Home and Apache 2.0.47 with PHP beta 2.

This bug is related to function/class/global scopes.

Note that if you merge foo.php and bar.php (foo.php and right after that bar.php) (ie. without the include_once), this will work as expected!

I haven't tested this with include but I suspect it would have the same problem.

Reproduce code:
// foo.php
class TestOne {
  function __construct() { include_once 'bar.php'; }
$TestOne = new TestOne();

// bar.php
// uncommenting this makes it work
// global $foo;
$foo = 'bar';
// uncommenting this does nothing
// global $foo;
function TestTwo() {
  global $foo; echo $foo;

Expected result:

Actual result:
No output


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-08 11:55 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

when you include a file within a function/method , the global code is in the function/method scope.

therefore $foo is in the function scope... (not global)..

functions/classes/methods you define in there however are defined globaly..
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Feb 24 11:01:28 2024 UTC