Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why won't this work?

Status
Not open for further replies.

Skute

Programmer
Jul 21, 2003
272
GB
Hi, I've got a problem with a class, can you tell me what the problem is?

[a.php]
<?php
$g_a = "hello";
?>

[b.php]

require_once("a.php");

echo $g_a;

class Test
{
public static function GetA()
{
if ($g_a == null || $g_a == "")
throw new Exception("$g_a is an invalid value!");

return $g_a;
}
}


When loading b.php the echo correctly outputs "hello", but when i use Test::GetA() an exception gets thrown every time. Why can't my static functions in the class view the global variable?

Thanks

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
because $g_a is not a superglobal variable. the fact that it is dimensioned and set outside the scope of a function or a class does not per se make it so global as to be available in the class or function. i think that "global" is a misnoma.

use the global language construct to make a global variable accessible within the scope of a function. or pass its value by reference to your constructor. or reference $GLOBALS['g_a'] instead.
 
OK, thanks, I'll try that. I have made the constructor private so that you can't instantiate an instance of it (it will have several static functions which just return values in another file)

Skute

&quot;There are 10 types of people in this World, those that understand binary, and those that don't!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top