Php 未捕获错误:调用成员函数错误消息
更新1: 我忘了添加Php 未捕获错误:调用成员函数错误消息,php,function,oop,Php,Function,Oop,更新1: 我忘了添加GetTags()方法,所以它是: public $blog_tags; public function GetTags() { return $this->blog_tags; } ========================================================================= 我正在使用PHP OOP开发我的项目。基本上,我有一个名为blogs的表,其中包含一些字段和数据,如下图所示: 然后我创建了一个类
GetTags()
方法,所以它是:
public $blog_tags;
public function GetTags()
{
return $this->blog_tags;
}
=========================================================================
我正在使用PHP OOP开发我的项目。基本上,我有一个名为blogs
的表,其中包含一些字段和数据,如下图所示:
然后我创建了一个类Blos.class.php
,并创建了两个方法,如下所示:
public function ShowTag()
{
$tag = $this->_db->prepare("SELECT blog_tags FROM blogs");
$tag->execute();
while($row = $tag->fetch())
{
$this->blog_tags = $row['blog_tags'];
}
}
public function NumTag()
{
$cat = $this->_db->prepare("SELECT blog_tags FROM blogs");
$cat->execute();
$row_cat = $cat->rowCount();
return $row_cat;
}
然后为了在屏幕上检索数据,我做了以下操作:
$tagSet = new Blogs();
$tags = $tagSet->NumTag();
$tagShow = $tagSet->ShowTag();
if(!empty($tags)){
$tagShow->GetTags();
}else{
echo "There is no tag available right now!";
}
但问题是,我收到了以下错误消息:
致命错误:未捕获错误:在第20行的null上调用成员函数GetTags()
这是哪一行:
$tagShow->GetTags();
那么这有什么错呢?您能帮我吗?这是您的代码:
if(!empty($tags)){
$tagShow->GetTags();
}else{
echo "There is no tag available right now!";
}
有
GetTags()
函数不是当前的类。如果在这个类中使用GetTags,那么使用$this->GetTags()
类函数的问题是
$tagShow = $tagSet->ShowTag(); //returns no value, hence $tagShow is null/empty.
而且
$tagShow->GetTags();
GetTags()是类中不存在的函数。另外,调用函数的方式也不对$tagShow最初不是类的对象,它是存储ShowTag()输出的变量
如果有一个名为GetTags()的函数,请使用
$tagSet->GetTags();
试着这样使用它:
public function ShowTag()
{
$blog_tags=array();
$tag = $this->_db->prepare("SELECT blog_tags FROM blogs");
$tag->execute();
while($row = $tag->fetch())
{
$blog_tags[] = $row['blog_tags'];
}
return $blog_tags;
}
然后在你的召唤中
$tagShow = $tagSet->ShowTag();
if(count($tagShow)>0){
print_r($tagShow); //Do whatever with the tags array.
}else{
echo "There is no tag available right now!";
}
试试这种方法,看看是否有帮助。然后它什么也不显示,没有输出,也没有错误!但是,数据库中存在一些行!您是否在类中添加了getTags()函数。如果是,请更新您的代码以便我检查。另外,尝试从ShowTags()返回一些内容并打印出来,看看它输出了什么。感谢您的回答,我完成了您所说的所有步骤,但仍然没有显示任何内容,我还更新了问题并添加了
GetTags()
method.OOPS!现在我明白了:),谢谢@拉帕伊斯塔利奥:很高兴这有帮助。