bla*_*tus 3 php namespaces global-variables
我不知道为什么我的HelperClass()下的全局$ error返回为空,我不知道为什么我可以验证$ class-> error确实填满了之前的数据.
在这种情况下,我不知道有关于命名空间的某些问题吗?请给我一些指示.
以下是一些相关的代码.
在主文件下
namespace Core;
$class = new ControllerClass();
$error = $class->error;
// verified that $error prints correctly here
include ViewFile.php;
Run Code Online (Sandbox Code Playgroud)
在ViewFile.php下
$helper = new HelperClass();
// __autoload function took care of the include
Run Code Online (Sandbox Code Playgroud)
在HelperClass下:
namespace Core\Skeleton;
class HelperClass {
public function __construct() {
global $error;
// $error != $class->error as defined earlier
// $error is empty here
}
Run Code Online (Sandbox Code Playgroud)
如果您正在使用自动加载器或在另一个辅助函数中包含您的类,则该$error变量从未在"全局"范围内声明.最终在一些地方,并得到了处置.
在为其分配值之前声明它已共享.
namespace Core;
$class = new ControllerClass();
global $error;
$error = $class->error;
Run Code Online (Sandbox Code Playgroud)
此外,虽然共享变量本身没有任何问题.这个名字$error似乎有点过于通用了.也许你可以使用一个不那么暧昧或更有条理的交换变量.$GLOBALS["/var/log"]["controller_error"]或类似的东西.
| 归档时间: |
|
| 查看次数: |
4616 次 |
| 最近记录: |