You are here: Re: [PHP] Global unavailable? « PHP « IT news, forums, messages
Re: [PHP] Global unavailable?

Posted by Jochem Maas on 10/17/73 11:28

Marcus Bointon wrote:
> I have a simple situation:
>
> in a.inc.php:
>
> $a = 1;
>
> in b.class.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):
>
> global $a;
> $a = 1;

if changing the declaration in a.inc.php fixes it then you must
NOT be including b.inc.php form the global scope. which means $a will not
be in the global scope unless you tell php to put it there, instead $a
is probably part of a function scope.

e.g. you have a function like so:

function getNewBee()
{
require_once('b.inc.php');
$b =& new b;
return $b;
}

in the above $a lives in the scope of the function call to
getNewBee() and NOT in the global scope.
>
> or by requiring the inc file inside each function of b (much less
> efficient):
>
> class b {
> function test() {
> require 'a.inc.php';

doing it like this means you don't need to specify
$a as 'global' - its already in the scope of the current function
(method), specifying it as 'global' will make it available everywhere
though.

> global $a;
> echo $a;
> }
> }
>
> Is this just how it is, or am I doing something wrong?
>
> Marcus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация