Globals unavailable, require oddity
| From: | Marcus Bointon | Date: | Thu, 13 Oct 2005 16:51:56 +0000 |
| Subject: | Globals unavailable, require oddity | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-19589@lists.php.net to get a copy of this message | ||
I posted this on general a while ago and the only conclusion anyone could come to is that there's a PHP bug somewhere. I'm running a snap of 5.1 from today on OS X 10.4.2, and I've seen the same symptoms on RedHat EL4. Before I report something does anyone here have any idea what could be amiss here?
I have a simple situation:
in a.inc.php:
<?php
$a = 1;
?>
in b.class.php:
<?php
require 'a.inc.php';
class b {
function test() {
global $a;
echo $a;
}
}
?>
With this pattern, $a is NOT visible within class b, even though it is declared in the global scope and I'm using the global keyword!
I can work around it two ways; by changing the original declaration (which just seems wrong - it's already in the global scope at this point):
<?php
global $a;
$a = 1;
?>
or by requiring the inc file inside each function of b (much less efficient):
<?php
class b {
function test() {
require 'a.inc.php';
global $a;
echo $a;
}
}
?>
What's going on here? I feel like I'm going nuts over something so simple.
I have another problem with included classes that seems to defy explanation:
<?php
require_once 'Config.php';
$a = new Config;
?>
Fatal error: Class 'Config' not found
but the error is from the new, not the require! This class is straight from PEAR, and I have /usr/local/libs/php in my include path which does contain Config.php. If I change the require to something that really doesn't exist, I get (as expected):
Fatal error: Failed opening required 'xConfig.php'
Is there some "stop_working=true" flag I've left in php.ini?
Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
marcus@synchromedia.co.uk | http://www.synchromedia.co.uk