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 回答

翻過高山走不出你
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

大話西游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 關注
- 530 瀏覽
添加回答
舉報
0/150
提交
取消