本文共 908 字,大约阅读时间需要 3 分钟。
我不知道为什么我的HelperClass()下的全局$error返回为空,我不知道为什么我可以验证$class->错误确实已经填满了之前的数据.
在这种情况下,我不知道有关于命名空间的某些问题吗?请给我一些指示.
以下是一些相关的代码.
在主文件下
namespace Core;
$class = new ControllerClass();
$error = $class->error;
// verified that $error prints correctly here
include ViewFile.php;
在ViewFile.php下
$helper = new HelperClass();
// __autoload function took care of the include
在HelperClass下:
namespace Core\Skeleton;
class HelperClass {
public function __construct() {
global $error;
// $error != $class->error as defined earlier
// $error is empty here
}
解决方法:
如果您正在使用自动加载器或在另一个辅助函数中包含您的类,则$error变量从未在“全局”范围内声明.最终在一些地方,并得到了处置.
在为其分配值之前声明它已共享.
namespace Core;
$class = new ControllerClass();
global $error;
$error = $class->error;
此外,虽然共享变量本身没有任何问题.名称$error似乎有点过于通用.也许你可以使用一个不那么暧昧或更有条理的交换变量. $GLOBALS [“/ var / log”] [“controller_error”]或类似的东西.
标签:php,global-variables,namespaces
来源: https://codeday.me/bug/20190723/1514421.html
转载地址:http://rhexl.baihongyu.com/