PHP-为什么下列代码会出现“ Using $this when not in object context”错误

UI设计界面 UI设计界面 主题:1059 回复:2190

PHP-为什么下列代码会出现“ Using $this when not in object context”错误

泛泛之交 发布于 2017-05-28 字数 1537 浏览 1237 回复 1

各位帮忙分析下,为什么下列代码会出现“ Using $this when not in object context”错误

<?php
class ExchangeRate{
static public $instance = NULL;
private $obsevers = array();
private $exchangeRate;

private function __construct(){
}
static public function getInstance(){
if(self::$instance == NULL){
self::$instance = new ExchangeRate();
}
return self::$instance;
}
public function registeObsever($obj){
$this->obsevers[] = $obj;
}
public function notifyObsevers(){
foreach($this->obsevers as $obj){
$obj->notify($this);
}
}
public function getExchangeRate(){
return $this->exchangeRate;
}
public function setExchangeRate($rate){
$this->exchangeRate = $rate;//此处报错
$this->notifyObsever();
}
}
interface Obsever
{
public function notify($obj);
}
class ProductItem implements Obsever
{
public function __construct(){
ExchangeRate::getInstance()->registeObsever($this);
}
public function notify($obj){
if($obj instanceof ExchangeRate){
print "Recieved Updata";
}
}
}

$product1 = new ProductItem();
$product2 = new ProductItem();
ExchangeRate::setExchangeRate(4.5);//调用报错
?>

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

支持 Markdown 语法,需要帮助?

评论(1

想挽留 2017-08-14 1 楼

ExchangeRate::getInstance()->setExchangeRate('4.5');