Rob*_*man 0 php class laravel laravel-4
我想使用函数addMenu和菜单,但我收到此错误:
Using $this when not in object context
Run Code Online (Sandbox Code Playgroud)
我做错什么了吗?还是有另一种方法来调用这些功能?
我的代码:
class Documentation {
protected $app;
protected $menu = [];
public function __construct(Application $app){
$this->app = $app;
$this->addMenu(["Hello world"]);
}
public static function addMenu($item){
$this->menu[] = $item;
}
public static function menu(){
return $this->menu;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
你不能$this
在静态上下文中使用变量.在你的情况下,我建议你改变你的功能,摆脱静态关键字
class Documentation {
protected $app;
protected $menu = [];
public function __construct(Application $app){
$this->app = $app;
$this->addMenu(["Hello world"]);
}
public function addMenu($item){
$this->menu[] = $item;
}
public function menu(){
return $this->menu;
}
}
Run Code Online (Sandbox Code Playgroud)