Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class inheritance in PHP 5.2: Overriding static variable in extension class?

I need to bea be able to use a static variable set in a class that extends a base class... from the base class.

Consider this:

class Animal {
    public static $color = 'black';

    public static function get_color()
    {
        return self::$color;
    }
}

class Dog extends Animal {
    public static $color = 'brown';
}

echo Animal::get_color(); // prints 'black'
echo Dog::get_color(); // also prints 'black'

This works wonderfully in PHP 5.3.x (Dog::get_color() prints 'brown') since it has late static binding. But my production server runs PHP 5.2.11 and so I need to adapt my script.

Is there a somewhat pretty workaround to solve this issue?

Cheers!
Christoffer

EDIT: The goal

As noted below, this is a very much simplified example of what I am trying to accomplish. If I provide you with the two options I have used to solve my problem (and the problem itself) someone might have a different solution than I...

I have built a base database model that contains functions like "find", "find_by" and "find_all" (all static).

In PHP 5.3 there is a function called get_called_class() which I currently use to determine the called class's name, and then use it to map against the correct database table. Ex class User would point to users.

get_called_class() doesn't exist in PHP 5.2.x and the hack implementations I've found are very unreliable. Then I turned to this option of using a static variable in all model classes which contain the class name.

like image 774
Christoffer Avatar asked Jun 10 '10 19:06

Christoffer


People also ask

Can we inherit static class in PHP?

In PHP, if a static attribute is defined in the parent class, it cannot be overridden in a child class.

Can we override static method in PHP?

Here comes the call of static method with static keyword. In case there is no overridden function then it will call the function within the class as self keyword does. If the function is overridden then the static keyword will call the overridden function in the derived class.

What is static keyword in PHP?

The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.

Why we use late static binding in PHP?

Late static bindings in PHP is a feature which can be used to reference the called class in a context of static inheritance. Essentially, late static bindings work by storing the class named in the last “non-forwarding call”.


3 Answers

I ran into this problem when subclassing something in the Zend Framework. My determination was that in purely static land, you only have one option... Redefine the function in the inheriting class:

class Animal {
    public static $color = 'black';

    public static function get_color()
    {
        return self::$color;
    }
}

class Dog extends Animal {
    public static $color = 'brown';

    public static function get_color() 
    {
      return self::$color;
    }
}

If you are able to create instances - You can use get_class($this) to find out the calling class, for example:

class Animal {
    public static $color = 'black';

    public function getColor() // note, not static
    {
      $class = get_class($this);
      return $class::$color;
    }
}

class Dog extends Animal {
    public static $color = 'brown';
}

$animal = new Animal();
echo $animal->getColor(); // prints 'black'
$dog = new Dog();
echo $dog->getColor(); // prints 'brown'

The only other option I thought of was using a function parameter to the static function to define a class it was being called from. It can default to __CLASS__ and then you can return parent::get_class($class) from the subclass. A pattern like this could be used to make re-using the static function easier (as I doubt returning a public static property is the only thing you are trying to use self:: for in that static method:

class Animal {
    public static $color = 'black';

    public static function get_color($class = __CLASS__)
    {
        // Super Simple Example Case... I imagine this function to be more complicated
        return $class::$color;
    }
}

class Dog extends Animal {
    public static $color = 'brown';

    public static function get_color($class = __CLASS__)
    {
      return parent::get_color($class);
    }
}
like image 147
gnarf Avatar answered Nov 15 '22 04:11

gnarf


In PHP 5.3+, the following would be preferred:

class Animal {
    public static $color = 'black';

    public static function get_color()
    {
        return static::$color; // Here comes Late Static Bindings
    }
}

class Dog extends Animal {
    public static $color = 'brown';
}

echo Animal::get_color(); // prints 'black'
echo Dog::get_color(); // prints 'brown'

Late Static Bindings (PHP.net)

like image 45
bristweb Avatar answered Nov 15 '22 04:11

bristweb


Sadly, before PHP 5.3 there's no way to simulate late static binding. The only way you can get the inheritance to work as you intend is if those are instance variables and methods.

like image 36
VoteyDisciple Avatar answered Nov 15 '22 05:11

VoteyDisciple