【发布时间】:2011-01-21 23:56:27
【问题描述】:
我有一个问题:
我正在编写一个没有框架的新 WebApp。
在我的 index.php 我正在使用:require_once('load.php');
在 load.php 中,我使用 require_once('class.php'); 来加载我的 class.php。
在我的 class.php 中出现此错误:
致命错误:当不在 class.php 的对象上下文中时使用 $this ...(在本例中为 11)
我的 class.php 是如何编写的示例:
class foobar {
public $foo;
public function __construct() {
global $foo;
$this->foo = $foo;
}
public function foobarfunc() {
return $this->foo();
}
public function foo() {
return $this->foo;
}
}
在我的 index.php 中,我可能正在加载 foobarfunc(),如下所示:
foobar::foobarfunc();
但也可以
$foobar = new foobar;
$foobar->foobarfunc();
为什么会出现错误?
【问题讨论】:
-
巧合的是,我昨天在这个错误中挣扎了大约 3 个小时! :)
-
@jack 在我的情况下,我在静态函数上使用 this
标签: php function class object fatal-error