Click to See Complete Forum and Search --> : Call to member function of a non-bject


Tischnoetentoet
December 27th, 2004, 05:20 AM
Hi all,

I have a class, say Item.

I have an array of objects of this items, say $m_arrItems.

I can work with the items in the array like $this->m_arrItems[$index]->ExecuteFunction();

However, now I want to do something special with one object, so I wrote a function for it.

private function SetSpecialID(&$object)
{
return $object->SetID(5);
}

No I get the error:
Call to a member function SetID() on a non-object.

I call the function like this:
$this->SetSpecialID($this->m_arrItems[$index]);

I think it has to so something with the reference, but I am not sure.
It seems that the type information is lost when I pass the refernence to the object.

Can anyone explain me what I am doing wrong?

Thanks!

visualAd
December 28th, 2004, 02:39 AM
I think you are right, the reference is being passed wrong. My guess is that the call to your function is actually passing a refernce to the array item rather which in turn contains the reference to the class. I do believe that by default all object passed as function parameters are passed as a refernece in PHP 5 so you could try the following:

private function SetSpecialID($object)

$this->SetSpecialID($this->m_arrItems[$index]);

Tischnoetentoet
December 28th, 2004, 09:58 AM
thanks for your answer.

To be sure we are talking about the same thing, I copied my real function (not an example).

my code is now:

private function ParentFromArrayToDatabase(Item $object, $arrArrayWithParent)
{
// Get parent in array
$iArrayParent = $object->GetParentInArray();

// Set new database parent
$object->SetParentInDatabase($arrArrayWithParent[$iArrayParent]->GetID());

// Save changes to database
$object->Commit();
}


Now, the object is known, because I put Item before the parameter. However, the array is still unknown :(

But unfortunatly I still get an error:

Notice: Undefined index: on line 739

Fatal error: Call to a member function SetID() on a non-object on line 739

visualAd
December 28th, 2004, 11:31 AM
Now I'm completely confused. The code you posted odesn't even contain a call to the SetId function :confused:

Tischnoetentoet
December 28th, 2004, 04:20 PM
I am sorry, I thought I was smart to make an example, but it wasn't smart after all...

So this is the new function:
private function ParentFromArrayToDatabase(Item $object, $arrArrayWithParent)
{
// Get parent in array
$iArrayParent = $object->GetParentInArray();

// Set new database parent
$object->SetParentInDatabase($arrArrayWithParent[$iArrayParent]->GetID());

// Save changes to database
$object->Commit();
}

The error is this:
Notice: Undefined index: on line 739

Fatal error: Call to a member function GetID() on a non-object on line 739

visualAd
December 29th, 2004, 11:59 AM
I am sorry, I thought I was smart to make an example, but it wasn't smart after all...

So this is the new function:
private function ParentFromArrayToDatabase(Item $object, $arrArrayWithParent) /* not valid */
{
// Get parent in array
$iArrayParent = $object->GetParentInArray();

// Set new database parent
$object->SetParentInDatabase($arrArrayWithParent[$iArrayParent]->GetID());

// Save changes to database
$object->Commit();
}

The error is this:
Notice: Undefined index: on line 739

Fatal error: Call to a member function GetID() on a non-object on line 739 The reason you are getting that error is because the index you are using to reference the array is invalid and will therefore be null. If the value is null it is not an object and trying to execte an object memeber function on it will result in a fatal error.

If I were you I would use some debugging echo statments to ensure the value being returned from the GetParentInArray() function is the one you expect.

Tischnoetentoet
December 31st, 2004, 02:27 AM
The reason you are getting that error is because the index you are using to reference the array is invalid and will therefore be null. If the value is null it is not an object and trying to execte an object memeber function on it will result in a fatal error.

If I were you I would use some debugging echo statments to ensure the value being returned from the GetParentInArray() function is the one you expect.


You are absolutely right! Somehow, another function was not setting the values, while I was expecting it to work!

Thanks for the help!