Page 1 of 1

Object becomes a non-object???

Posted: Thu Sep 18, 2008 8:57 am
by Los Frijoles
Here is my code:

Code: Select all

public static function restore($color)
{
$db = DbConnection::getDbConnection(); //line 71
$sql = $db->Prepare('select groupId, hexValue, colorName from Color where colorId = ? limit 1'); //line 72
try //line 73
{ //line 74
    echo is_a($color, "Color"); //line 75
    $rs = $db->execute($sql, array($color->getColorId())); //line 76
} //line 77
catch (exception $e)  //line 78
{ //line 79
    print_r($e); //line 80
} //line 81
...
The function is inside the class Color.

The purpose of the code is to retrieve certain values from a MySQL database based on an Id (colorId in this case). This problem has also cropped up in some of my other classes, but this is the least lengthy one and the easiest to show. Basically, what the problem is is that I get the following error when I try to execute the above code:

Code: Select all

[Thu Sep 18 07:48:40 2008] [error] [client 10.25.100.99] PHP Fatal error:  Call to a member function getColorId() on a non-object in /srv/www/htdocs/scheduler/dataAccess/Color.class.php on line 76
However, the echo is_a... line outputs a 1 which means that it should be an instance of the Color class. This doesn't make sense...how can an object suddenly become a non-object???

Re: Object becomes a non-object???

Posted: Thu Sep 18, 2008 9:28 am
by panic!
Why don't you var_dump($color) and see what happens?

Re: Object becomes a non-object???

Posted: Thu Sep 18, 2008 10:14 am
by Jade
Are you carrying the object across several pages, or form posts? If so you need to make sure you save the object in a session or restore it on every page.

Re: Object becomes a non-object???

Posted: Thu Sep 18, 2008 10:43 am
by Maugrim_The_Reaper
Where is the $color passed from? Maybe use type hinting to locate the invalid pass using:

Code: Select all

public static function restore(Color $color)
That should give an error showing where the non-object is being passed from - i.e. the originating point of the value of $color. Likely that's where the suspect code is located if the app is simple enough. Might need to do more digging, like getting a trace (look up installing XDebug which can provide more detailed error messages with full code profiling showing the execution trace).

Re: Object becomes a non-object???

Posted: Thu Sep 18, 2008 10:59 am
by Christopher
I think the line should be:

Code: Select all

    $rs = $db->execute($sql, array($this->getColorId())); //line 76

Re: Object becomes a non-object???

Posted: Thu Sep 18, 2008 11:05 am
by Jade
Yes but $color isn't an variable in that class, it's being passed in as a parameter.

Re: Object becomes a non-object???

Posted: Thu Sep 18, 2008 11:37 am
by Maugrim_The_Reaper
Can't use $this inside a static context anyway.

Re: Object becomes a non-object???

Posted: Thu Sep 18, 2008 1:15 pm
by Christopher
Maugrim_The_Reaper wrote:Can't use $this inside a static context anyway.
I was totally off on that! :drunk:

Are you sure that there is a method named (exactly ;)) Color::getColorId()

Re: Object becomes a non-object???

Posted: Thu Sep 18, 2008 1:47 pm
by Maugrim_The_Reaper
Ah, but we forgive these minor slips from you, oh Master :).

Re: Object becomes a non-object???

Posted: Thu Sep 18, 2008 2:08 pm
by Christopher
Maugrim_The_Reaper wrote:Ah, but we forgive these minor slips from you, oh Master :).
You mean frequent and glaring! ;) No ... you are oh Master! :)