Object becomes a 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
Los Frijoles
Forum Newbie
Posts: 1
Joined: Thu Sep 18, 2008 8:50 am

Object becomes a non-object???

Post 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???
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: Object becomes a non-object???

Post by panic! »

Why don't you var_dump($color) and see what happens?
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Object becomes a non-object???

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Re: Object becomes a non-object???

Post 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).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Object becomes a non-object???

Post by Christopher »

I think the line should be:

Code: Select all

    $rs = $db->execute($sql, array($this->getColorId())); //line 76
(#10850)
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Object becomes a non-object???

Post by Jade »

Yes but $color isn't an variable in that class, it's being passed in as a parameter.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Re: Object becomes a non-object???

Post by Maugrim_The_Reaper »

Can't use $this inside a static context anyway.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Object becomes a non-object???

Post 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()
(#10850)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Re: Object becomes a non-object???

Post by Maugrim_The_Reaper »

Ah, but we forgive these minor slips from you, oh Master :).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Object becomes a non-object???

Post 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! :)
(#10850)
Post Reply