在我们的旧项目中,有这样的代码:
class X {
var title = "";
}
class Y extends X {
echo $this->title;
}
class Z extends Y {
function printTitle(){
echo "<b>".$this->title."</b>;
}
}
在 PHP 5.4 中它工作正常,但今天我在服务器上安装了 PHP7.0.3 并开始测试应用程序。
现在我面对这条错误消息:
Fatal error: Uncaught Error: Using $this when not in object context in ...
我完全不知道为什么这会产生这个致命错误。
我该如何解决它?
class X {
private $title;
public function __construct($title){
$this->title = $title;
}
public function getTitle() {
return $this->title;
}
}
class Y extends X {
}
class Z extends Y {
public function printTitle(){
echo "<b>".$this->getTitle()."</b>;
}
}
$z = new Z("My title");
$z->printTitle(); //echo "<b>My title</b>"
$z->getTitle(); //Returns "My title"