Home >Backend Development >PHP Problem >Solve the problem of this error in php
When I called the attribute, no error was reported in php5, but an error was reported in php7. The code is as follows
<?php class test { protected $view; protected function testxxx() { $this->view = xxx; } }
Another class
<?php cless xxx extends test { public function xx() { self::testxxx() } }
The error message is as follows:
[29-Dec-2017 23:21:45 Asia/Shanghai] PHP Fatal error: Uncaught Error: Using $this when not in object context in
The answer has been solved. It was caused by using a variable function somewhere. The solution is
call_user_func([new xxx, '函数名'])
In addition, everyone said that $this should be used , instead of self, the explanation is as follows:
public: self::var 调用父类方法或者属性 parent::method 调用父类方法 实例中可以通过$this->var 调用public类型的方法或属性 protected: self::var 调用父类方法或者属性 parent::method 调用父类方法 实例中不可以通过$this->var 调用public类型的方法或属性 private: 只能在该类中使用
Based on the above points, it is OK to use this and self in this instance.
Recommended tutorial: "php tutorial"
The above is the detailed content of Solve the problem of this error in php. For more information, please follow other related articles on the PHP Chinese website!