I have the following (stripped down) code:
<?PHP
class A {
function Show(){
echo "ciao";
}
}
$a = new A();
$b = new B();
class B {
function __construct() {
$a->Show();
}
}
?>
With a bit of surprise I cannot access the globally defined $a variable from within the class and I get a Undefined variable exception. Any help?
Why the surprise? That's a pretty logical variable scope problem there...
I suggest you use either the
global
keyword or the variable$GLOBALS
to access your variable.EDIT: So, in your case that will be:
or
EDIT 2: And, since Vinko is right, I suggest you take a look at PHP's manual about variable scope.