Php 对非对象调用成员函数

Php 对非对象调用成员函数,php,Php,我想显示有关对象活动的信息,来自列表活动的函数getCurrent()应该返回这些信息。 当我尝试它时,它工作得非常好,我从类中获得了所需的信息,但是,我在页面顶部有一条错误消息: 致命错误:在上调用成员函数GetIdentification() 非宾语 /Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php 在线34 第34行在这里: while($listActivities->next())

我想显示有关对象
活动
的信息,来自
列表活动
的函数
getCurrent()
应该返回这些信息。 当我尝试它时,它工作得非常好,我从类中获得了所需的信息,但是,我在页面顶部有一条错误消息:

致命错误:在上调用成员函数GetIdentification() 非宾语 /Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php 在线34

第34行在这里:

  while($listActivities->next())
  {
     $current = new Activity();
     $current = $listActivities->getCurrent();
     echo $current->getId(); // line 34
  }
这是返回活动对象的
getCurrent()
函数

  public function getCurrent()
  {
     if(isset($this->activities[$this->current]))
        return $this->activities[$this->current];
  }
我不明白为什么我有这个问题,因为它返回我想要的对象。 请帮我弄清楚。谢谢

echo $current->getId(); // line 34
Fatal error: Call to a member function getIdentifiant() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php on line 34
无论您认为发生了什么,或者您在页面中看到了什么,如果错误表明$current不是对象,则它不是。它可能不是null,但也可能是数组或任何非对象的对象

此外:

$current = new Activity();
$current = $listActivities->getCurrent();
对我来说没什么意义

使用

看看它到底返回了什么,相信错误说了什么


编辑:您甚至可能没有准确地查看正确的php脚本:错误显示为“getIdentifiant”,而代码显示为“getId”。确保您正在查看正确的代码并刷新正确的页面。

第一组
$this->current
之后
$current=new Activity()(可能在constrercture中)

如果
,则应返回false!isset($this->activities[$this->current])

另外如果您使用
$current=$listActivities->getCurrent()
您丢失了
Activity
对象,它应该保存到另一个变量中

新代码如下:

  while($listActivities->next())
  {
     $current = new Activity();
     if(  $listActivities->getCurrent() )
        echo $current->getId(); // line 34
  }


  public function getCurrent()
  {
     if(isset($this->activities[$this->current]))
        return $this->activities[$this->current];
     return false;
  }

当isset失败时,getCurrent()返回什么<代码>空值
?这不是一个对象。@bwoebi它返回一些东西,因为我在页面中看到了有关活动的信息。它绝对不会返回null。感谢您的回复。请在回音之前尝试
var_dump()
$current
。@R00t-然后向我们展示正确的代码,因为如果isset()失败,您发布的代码不会返回任何内容,但您坚持它会返回一些内容;因此,您显示的代码不能是正确的代码可能与感谢您的回复重复!我写了
$current=newactivity
,因为我认为问题在于此。但是,似乎
getCurrect()
正在返回
'object(Activity)#7(4){[“id”:“Activity”:private]=>string(3)“fir…blablablablablabla…}**NULL**
。我猜列表中的最后一个对象是NULL,对吗?很好!它工作了。实际上,函数
getCurrent()
总是为列表的最后一个对象返回“null”。因此真正的问题是($listActivities->next())
不应该调用实际的最后一个对象(或null)。但是谢谢你,非常感谢你的帮助:)
  while($listActivities->next())
  {
     $current = new Activity();
     if(  $listActivities->getCurrent() )
        echo $current->getId(); // line 34
  }


  public function getCurrent()
  {
     if(isset($this->activities[$this->current]))
        return $this->activities[$this->current];
     return false;
  }