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

Posted by Marcus Bointon on 10/21/24 11:28

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;

or by requiring the inc file inside each function of b (much less
efficient):

class b {
function test() {
require 'a.inc.php';
global $a;
echo $a;
}
}

Is this just how it is, or am I doing something wrong?

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

 

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

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