Reputation: 131
I am stuck with this confusion where I don't understand why my global $error under my HelperClass() returns empty, where I could verify that $class->error is indeed filled up with data earlier on.
Is there some sort of issues with namespace in this case that I am not aware about? Please give me some pointers.
Here are some of the codes that are relevant.
Under Main file
namespace Core;
$class = new ControllerClass();
$error = $class->error;
// verified that $error prints correctly here
include ViewFile.php;
Under ViewFile.php
$helper = new HelperClass();
// __autoload function took care of the include
Under HelperClass:
namespace Core\Skeleton;
class HelperClass {
public function __construct() {
global $error;
// $error != $class->error as defined earlier
// $error is empty here
}
Upvotes: 3
Views: 5696
Reputation: 145482
If you're using an autoloader or include your classes from within another helper function, then the $error
variable was never declared in the 'global' scope. It ended up in some local, and got disposed.
Declare it shared right before you assign it a value.
namespace Core;
$class = new ControllerClass();
global $error;
$error = $class->error;
Also while there is nothing wrong with shared variables per se. The name $error
seems slightly too generic. Maybe you can up with a less ambigious or more structured exchange variable. $GLOBALS["/var/log"]["controller_error"]
or something arrayish.
Upvotes: 8