使用mysql_fetch_assoc()在PHP中构建继承的对象

I've recently started using mysql_fetch_object() when looping through objects in my personally developed CMS.

I used to loop through using while($row = mysql_fetch_assoc($rows)) { ... }

and then I'd create an object and assign the values to it in the loop:

$object = new Object(); 
$object->set_by_row($row); // Does what it says

What i really want to do is use:

while ($object = mysql_fetch_object($rows, 'self')) { ... }

I hhve also tried using:

while ($image = mysql_fetch_object($rows, 'Image')) { ... } // Actual class name

But I can't! Most of my classes are subclasss of a parent class called Identifiable, which is a collection of variables and functions that apply to all my subclasses. It stores the ID of each object too - so when I use mysql_fetch_assoc(), only the variables set in that class are set, not the associated parent variables.

Anybody know of a way to use mysql_fetch_object to set objects including their inherited variables?

Note: This is all within a static function within the subclass.

Here's a var_dump from after the assignment line:

object(Image)#5 (16) {
    ["title":"Image":private]=> string(0) ""
    ["location":"Image":private]=> string(0) ""
    ["description":"Image":private]=> string(0) ""
    ["file_name":"Image":private]=> string(14) "0_IMG_0984.JPG"
    ["original_x":"Image":private]=> string(4) "2592"
    ["original_y":"Image":private]=> string(4) "1944"
    ["type":"Image":private]=> string(3) "jpg"
    ["id"]=> string(1) "1"
    ["added_by"]=> string(1) "1"
    ["updated_by"]=> string(1) "0"
    (...) ["deleted_by"]=> string(1) "0" ["deleted"]=> string(1) "0" ["published"]=> string(1) "0" ["datetime_added"]=> string(19) "2012-02-25 22:51:08" ["datetime_updated"]=> string(19) "0000-00-00 00:00:00" ["datetime_deleted"]=> string(19) "0000-00-00 00:00:00"
}

As you can see, ID is in there, but my getter method is in the parent class, and returns nothing.

The getter method in the Identifiable class is here:

function get_id()
{
    return $this->id;
}

CRITICAL UPDATE:

After I have set the object: while ($image = mysql_fetch_object($rows, 'Image')) { ... } I tried calling echo $image->get_id(); and it works fine!

However, my problem still exists: I currently call a function in the loop (get_interface_preview()) that actually contains the call to $this->get_id() and that still returns nothing.

COMPLETE OVERHAUL!

My problem is here:

function get_interface_preview()
{
    echo $this->get_id();
    return "<a href='?a=edit&id='".$this->get_id()."'><img src='".IMAGE_PATH."library/admin_thumb/".$this->file_name."'></a>";
}

It echos the ID fine, but the ID isn't getting added to the markup.. I feel I have made a HUGE rookie mistake somehow?!

FIXED IT! I am so sorry everyone, this was an apostrophe in the wrong place! SO SORRY!

I put an apostrophe in the wrong place. Feel free to throw humiliation my way!

You have to give it a class name, 'self' is not a class name. Use get_class($this) to get the class name of the current object, so:

while ($object = mysql_fetch_object($rows, get_class($this))) { ... }

Or, when in a static method, use get_called_class():

while ($object = mysql_fetch_object($rows, get_called_class())) { ... }

See also this PHP documentation contribution on a self-contained Singleton base class