What is the correct way to access the static properties of subclasses in static superclass methods in PHP?

Say I have the following:

<?php
abstract class MyParent
{
    public static $table_name;

    public static get_all(){
        return query("SELECT * FROM {$this->table_name}");
    }

    public static get_all2(){
        return query("SELECT * FROM ".self::table_name);
    }
}

class Child extends MyParent
{   public static $table_name = 'child'; }
?>

Assuming that it is querycorrectly defined, none of these methods does what I want: get_all () throws Fatal error: Using $this when not in object context in /path/to/foo.php on line xx, because it $thisis an instance variable.

and get_all2 () throws Fatal error: Undefined class constant 'table_name' in /path/to/foo.php on line xxbecause it is selfstatically determined.

It seems that such a thing is a whole inheritance, so it should be possible, at least easily, if not elegantly. (This is still PHP)

What should I do?

+3
source share
2 answers

self::table_name self::$table_name - . - PHP 5.3:

http://php.net/manual/en/language.oop5.late-static-bindings.php

self , proparty, , "" . "static" .

+5

self::$table_name, , , static::$table_name.

+4

All Articles