3

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
}

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.