Page 1 of 1

call to member function on non-object.

Posted: Sun Aug 21, 2011 11:45 am
by nonMember
I have got a problem thats been driving me nuts.
Here is the code snippet :

$contact = $contacts[0];
echo get_class($contact) . " " .$contact->getContactId(). " " . $contact->getFirstName(); <----- line 120

and the output and error im getting is :

Contact 101 Joe
Fatal error: Call to a member function getContactId() on a non-object in /var/www/html/sri1/orderDetails.php on line 120

The thing thats baffling me is that $contact is recognized as the right class, i.e. Contact, and $contact->getContactId() and $contact->getFirstName() are returning the values im expecting it to return, yet im getting the error saying im calling getContactId from a non-object, eventhough the get_class function recognizes the object as an instance of the correct class, and the function call is even working right. Could anyone help me with what im missing here, before i pull all my hair out.

Thanks.

Re: call to member function on non-object.

Posted: Sun Aug 21, 2011 1:53 pm
by genix2011
Hi,

try print_r($contact), this will give you more information about the variable.

Greets.

Re: call to member function on non-object.

Posted: Sun Aug 21, 2011 2:09 pm
by nonMember
The object is what i expect:
Result of:
$contact = $contacts[0];
echo print_r($contact) . " " .$contact->getContactId(). " " . " " . $contact->getFirstName();


Contact Object ( [contactId:private] => 101 [lastName:private] =>Smith [firstName:private] => Joe [phoneNumber:private] => 2345543221 [email:private] => joesmith@google.com [address:private] => 0 [company:private] => 0 [notes:private] => ) 1 101 Joe
Fatal error: Call to a member function getContactId() on a non-object in sri1/orderDetails.php on line 120


Contact class has the functions i called :

public function getContactId() {
return $this->contactId;
}


public function getFirstName() {
return $this->firstName;
}

Re: call to member function on non-object.

Posted: Mon Aug 22, 2011 1:32 am
by kasuals
Looking at your print_r and your function declarations everything should work fine. You may have to post more code, because the print_r returns a valid object... and if your functions are defined properly then they should (as you stated) return the proper private property value.