我只是试图通过构造函数来设置post_id,并通过另一个返回的function.But来获取id:Fatal error: Using $this when not in object context
,但是我不知道为什么会发生这种情况,我以前已经做过很多次了,但是现在出错了。
下面是代码
class PostData
{
private static $instance = null;
public $post_id = 0;
public function __construct($post_id = 0){
if((int)$post_id > 0){
$this->setId($post_id);
}
}
private function setId($post_id){
return $this->post_id = $post_id;
}
public static function getPostID(){
return $this->post_id;
}
public static function getInstance(){
if (empty(self::$instance)) {
self::$instance = new self();
}
return self::$instance;
}
}
以及我如何调用这个类
$post = new PostData(33);
echo $post->getPostID();
但是有一个错误:Fatal error: Using $this when not in object context
发布于 2018-06-07 08:59:55
问题是,您已经将函数声明为静态函数,然后在静态函数中使用关键字$this。
从要修复的函数中删除关键字static:
public function getPostID(){
return $this->post_id;
}
https://stackoverflow.com/questions/50731551
复制相似问题