Re: class problem :(
| From: | Nathan Nobbe | Date: | Thu, 28 May 2009 22:38:17 +0000 |
| Subject: | Re: class problem :( | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-293332@lists.php.net to get a copy of this message | ||
On Thu, May 28, 2009 at 4:28 PM, Luke <luke@blog-thing.com> wrote:
> Right I've read the manual on this and all that so hopefully you find
> people
> can help.
> I have an abstract class with three children. The abstract is ForumObject
> and the three children are Thread, Category and Post and each have their
> own
> table so I wrote the following:
> abstract class ForumObject
> {
> static private $table;
>
> static function getObjectIds ($field, $value)
> {
> $query = "SELECT id FROM {self::$table} WHERE $field = '$value'";
> $object_ids = mysql_fetch_array();
> return $object_ids;
> }
> }
>
> class Category extends ForumObject
> {
> static private $table = "categories";
> }
> That's just got the important bits for the sake of your eyes but basically
> the problem I'm having is calling
> Category::getObjectIds ($whatever, $whatever2);
> Seems to think that it's referring to ForumObject::$table rather than
> Category::$table?
> I looked into it and there seems to be something you can do with
> get_called_class() but unfortunately I'm stuck with 5.2.9 at the moment and
> that is new to 5.3.
> Any ideas? Perhaps there is a different way I could implement the classes -
> I would rather not have getObjectIds repeated three times!
> Thanks in advance,
this is a limitation in pre-5.3 php wherein there is no support for static
inheritance. another way to do it in 5.3, inside of the
ForumObject::getObjectId() method is to use
static::$table which will correctly resolve the lookup to the class youd
expect.
-nathan