call to member function on non-object.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nonMember
Forum Newbie
Posts: 2
Joined: Sun Aug 21, 2011 11:33 am

call to member function on non-object.

Post 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.
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: call to member function on non-object.

Post by genix2011 »

Hi,

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

Greets.
nonMember
Forum Newbie
Posts: 2
Joined: Sun Aug 21, 2011 11:33 am

Re: call to member function on non-object.

Post 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;
}
User avatar
kasuals
Forum Commoner
Posts: 28
Joined: Thu Aug 11, 2011 12:59 pm

Re: call to member function on non-object.

Post 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.
Post Reply