Re: class problem :(

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Luke 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,

I didn't test this, just a thought.  You'd still have to implement
getObjectIds(), but it would be slim:

abstract class ForumObject
{
  static private $table;

  static function getObjectIds ($field, $value, $class)
  {
    $query = "SELECT id FROM {$class::$table} WHERE $field = '$value'";
    $object_ids = mysql_fetch_array();
    return $object_ids;
  }
}

class Category extends ForumObject
{
  static private $table = 'categories';

  static function getObjectIds ($field, $value)
  {
    parent::getObjectIds ($field, $value, __CLASS__)
  }
}

Or probably the same because you're defining $table anyway in the child
class:

abstract class ForumObject
{
  static private $table;

  static function getObjectIds ($field, $value, $table)
  {
    $query = "SELECT id FROM $table WHERE $field = '$value'";
    $object_ids = mysql_fetch_array();
    return $object_ids;
  }
}

class Category extends ForumObject
{
  static private $table = 'categories';

  static function getObjectIds ($field, $value)
  {
    parent::getObjectIds($field, $value, self::$table)
  }
}

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux