# Globals unavailable, require oddity

- Thread: https://externals.io/message/19589
- Original archive: https://news-web.php.net/php.internals/19589
- Started by Marcus Bointon on 2005-10-13T16:51:56+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #19589 — Marcus Bointon — 2005-10-13T16:51:56+00:00

URL: https://externals.io/message/19589#19589

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

---

## #19590 — Antony Dovgal — 2005-10-13T17:01:58+00:00

URL: https://externals.io/message/19589#19590 · In reply to: #19589

> in a.inc.php:
> 
> <?php
> $a = 1;
> ?>
> 
> in b.class.php:
> 
> <?php
> require 'a.inc.php';
> class b {
>      function test() {
>          global $a;
>          echo $a;
>      }
> }
> ?>

Works fine here.

> <?php
> require_once 'Config.php';
> $a = new Config;
> ?>
> 
> Fatal error: Class 'Config' not found

Use absolute path everywhere ?
I can't reproduce it either.

-- 
Wbr, 
Antony Dovgal

---

## #19593 — Marcus Bointon — 2005-10-13T19:11:15+00:00

URL: https://externals.io/message/19589#19593 · In reply to: #19589

> I have another problem with included classes that seems to defy  
> explanation:

As usual there is a simple explanation for this one: I had a file  
called 'config.php' lurking in one of the folders in my include path  
- Deploying on Linux, I'd just got so used to assuming everything was  
case sensitive I'd forgotten that it ain't necessarily so on OS X.

The scope problem is still freaking me out though.

Marcus
-- 
Marcus Bointon
Synchromedia Limited: Putting you in the picture
marcus@synchromedia.co.uk | http://www.synchromedia.co.uk

---

## #19610 — oliver.graetz@arcor.de — 2005-10-14T01:25:11+00:00

URL: https://externals.io/message/19589#19610 · In reply to: #19589

Marcus Bointon schrieb:
> I posted this on general a while ago and the only conclusion anyone  
> could come to is that there's a PHP bug somewhere. 

Hm? I tested you code on a Debian 5.1 RC1 system and could not reproduce
the problem. I thought you failed to post the whole code you used since
you do no where in your code use the class so I have to guess what you
did about using it. My test case used your code and $a was visible. Did
you try it with the RC1 of PHP 5.1?

OLLi


____________
"Forget the Freud and stay with the drugs."
[Paul on DH 106]

---

## #19611 — Unknown W. Brackets — 2005-10-14T06:07:17+00:00

URL: https://externals.io/message/19589#19611 · In reply to: #19589

If you include a file within a function, that file will be loaded in 
that function's scope.  Example:

<?php

function test()
{
    include 'a.inc.php';
}

test();

echo isset($a) ? '$a is set' : '$a is not set', ".\n";

include 'a.inc.php';

echo isset($a) ? '$a is set' : '$a is not set', ".\n";

?>

The same happens if b.class.php is included within a function (or class 
member, etc.) and includes a.inc.php, as in your example (I am presuming.)

If this is the problem, I suggest simply adding:

global $a;

At the top of a.inc.php, which will solve the problem for you.

-[Unknown]

---

## #19612 — Derick Rethans — 2005-10-14T07:20:02+00:00

URL: https://externals.io/message/19589#19612 · In reply to: #19590

> 
> 
> > […]
> 
> Works fine here.

I can't reproduce it either with 4.4.0rc2-dev - and nothing from 
valgrind. Our huge application doesn't have any problems with it either.

Derick
-- 
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org

---

## #19613 — Marcus Bointon — 2005-10-14T08:09:30+00:00

URL: https://externals.io/message/19589#19613 · In reply to: #19611

> If you include a file within a function, that file will be loaded  
> in that function's scope.  Example:

> The same happens if b.class.php is included within a function (or  
> class member, etc.) and includes a.inc.php, as in your example (I  
> am presuming.)
>
> If this is the problem, I suggest simply adding:
>
> global $a;
>
> At the top of a.inc.php, which will solve the problem for you.

I'm aware of how it's supposed to work, but the problem is that it's  
doing something very screwy for me and not working like it should.  
Because it's so different to how it normally works, I can only  
conclude that it's a php.ini problem (though I can't think what  
setting would cause this) or a bug. I can work around it by declaring  
it as a global in the inc file, but my point is that it should not be  
necessary - something odd is going on that's peculiar to my setup.  
Given that no-one else can reproduce it, I'll just have to persevere.

Thanks for looking anyway,

Marcus
-- 
Marcus Bointon
Synchromedia Limited: Putting you in the picture
marcus@synchromedia.co.uk | http://www.synchromedia.co.uk

---

## #19614 — Derick Rethans — 2005-10-14T08:17:17+00:00

URL: https://externals.io/message/19589#19614 · In reply to: #19613

> I'm aware of how it's supposed to work, but the problem is that it's doing
> something very screwy for me and not working like it should. Because it's so
> different to how it normally works, I can only conclude that it's a php.ini
> problem (though I can't think what setting would cause this) or a bug. I can
> work around it by declaring it as a global in the inc file, but my point is
> that it should not be necessary - something odd is going on that's peculiar to
> my setup. Given that no-one else can reproduce it, I'll just have to
> persevere.

This can't be related to php.ini, but, perhaps you have a zend extension 
or another third party extension loaded?

Derick

-- 
Derick Rethans
http://derickrethans.nl | http://ez.no | http://xdebug.org
