PHP致命错误:在没有对象上下文时使用$this我有个问题:我正在编写一个没有框架的新网络应用程序。在我的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 回答
![?](http://img1.sycdn.imooc.com/545850c80001ebf202200220-100-100.jpg)
翻过高山走不出你
TA贡献1875条经验 获得超3个赞
public function foobarfunc() { return $this->foo();}
foobar::foobarfunc();
static
)$this
.
不应对非静态方法使用静态调用。 静态方法(或静态调用的方法)不能使用$this,它通常指向类的当前实例,因为在使用静态调用时没有类实例。
$foo
$foobar = new foobar();$foobar->foobarfunc();
__construct
global $foo;
global
$foo
$foo
$foo
$this->foo
![?](http://img1.sycdn.imooc.com/545861f00001be3402200220-100-100.jpg)
大话西游666
TA贡献1817条经验 获得超14个赞
foobarfunc
::
$this
$this
E_STRICT
Strict Standards: Non-static method foobar::foobarfunc() should not be called statically
$fb = new foobar;echo $fb->foobarfunc();
global
- 3 回答
- 0 关注
- 501 浏览
添加回答
举报
0/150
提交
取消