我上过这样的课:
<?php
class acController {
protected $_enabledPages = array(
'front_login.php',
);
protected $_enabledDirectories = array (
'admin'
);
public static function isAuthorized() {
echo '<pre>';
acController::checkResource($_SERVER['SCRIPT_URI'], $this->_enabledDirectories);
acController::checkResource($_SERVER['SCRIPT_URI'], $this->_enabledDirectories);
echo '</pre>';
}
protected static function checkResource($urlAddress, $addressArray) {}
}?>
我得到了一个错误:
致命错误:在非对象上下文中使用$this
但是,在这种情况下,类中使用了$this,而我不知道问题在哪里。在其他文件中,我使用acController::isAuthorized();获取信息
向你问好乔治!
发布于 2014-05-27 19:52:17
在静态函数中,不能使用$this。
$this暗示存在一个对象(类的实例)。而静态意味着对类的调用。
来自PHP文档:
因为静态方法在没有创建对象实例的情况下是可调用的,所以伪变量$this在声明为静态的方法中不可用。
发布于 2014-05-27 19:52:41
你有问题,因为你的方法是静态的。这表示类的一个实例,但如果将其作为静态使用,则可以在不使用类的任何实例的情况下使用这些方法。所以你不能用静力学来做这个。
https://stackoverflow.com/questions/23897690
复制相似问题