Sunday, 11 June 2017

php - Call a member function on a non object



i want to display informations about object Activity that the function getCurrent() from the ListActivity should returns.
When i try it, it works perfectly, i have the information needed from the class, but, i have this error message on the top of the page :




Fatal error: Call to a member function getIdentifiant() on a

non-object in
/Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php
on line 34




Line 34 is here :



  while($listActivities->next())
{
$current = new Activity();

$current = $listActivities->getCurrent();
echo $current->getId(); // line 34
}


And this is the getCurrent() function which return an Activity object.



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

return $this->activities[$this->current];
}


I don't understand why i have this problem since it returns me the object that i want.
Please help me figuring it out. Thanks.


Answer



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



WhatEVER you think happens, or whatEVER you see in your page, if the error says $current is not an object, it is not. It might not be null, but it could also be an array or anything that is not an object.



Also :



$current = new Activity();
$current = $listActivities->getCurrent();


Doesn't really makes sense to me.




Use



var_dump($listActivities->getCurrent());


To see what it exactly returns, and trust what errors say.



EDIT : And you may not even be looking at the right php script acually : The error says "getIdentifiant" while the code says "getId". Make sure you're looking at the right piece of code and refreshing the right page.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...