Inheritance of static member variables

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I've a class that defines a number of DB operations, and I need to do
something very similar but operating on a different table.

I had wanted to do something like the following:


abstract class BaseClass{
  const table_name;

  public static function getFoo(){
     $sql = "GET foo FROM ".self::table_name." WHERE 1";
     // return a result

class SubClass{
  const table_name='bar';


However, this doesn't work.  I see the reasons have been talked about a
lot on this group so I won't go into that discussion again!

I realise that I could instance the class and make the method
non-static, but the problem is that the existing class is already
widely used in a static context so I can't change that at this stage
unless I somehow provide a static call too.

My question is, how can I do something similar?  How can I keep all the
shared functionality in the base class and just define a few env.
variables in the subclasses without having to repeat my methods?



Re: Inheritance of static member variables

Some people think this is a bug in PHP and other's don't.  Check out
the following:

I guess it has to do with the way that self:: is bound (apparently, at
runtime, self is bound to the class in which is it defined rather than
the inheriting class).

The most obvious solution would be to define some abstract static
method in BaseClass called getTableName() or something, then define it
in each inheriting (child) class.  Unfortunately, you tend to run into
the same problem.

Although it may be undersirable, another alternative would be to define
the constant table_name in the child class, then have getFoo() take the
table name as a parameter inside BaseClass.  Although you'd then have
to override getFoo() in each child, it would be simple:

public static function getFoo() {
    return parent::getFoo(self::table_name);

For safety sake, you can also declare getFoo() as protected inside
BaseClass.  Again, not the most desirable solution (I'd prefer to see
the "bug" fixed) but I have yet to see a better way.

Anyone else have any thoughts?

Re: Inheritance of static member variables

Hm, thanks for that Zeldorblat.

One other slight wrinkle is that I have more than one constant in the
class that needs accessing (I was just simplifying for clarity), so
it'd be better if I could pass the entire class rather than the const
value.  Is there any way of doing that?  Trying to pass 'self' isn't
particularly successful, I've resorted to:


abstract class BaseClass{
  public static function getFoo($class){
     $sql = "GET foo FROM ".$class->table_name." WHERE 1";
     // return a result

class SubClass extends BaseClass{
  public $table_name='bar';
  public static function getFoo(){
     return parent::getFoo(new self());


But having to instance the class seems quite messy.

It's a real shame there isn't another token that works the way a lot of
people seem to expect 'self' to work!  Maybe no_really_I_mean_self:: ?


Site Timeline