Why can't you call abstract functions from abstract classes in PHP? Why can't you call abstract functions from abstract classes in PHP? php php

Why can't you call abstract functions from abstract classes in PHP?


This is a correct implementation; you should use static, not self, in order to use late static bindings:

abstract class AbstractFoo{    public static function foo() {        throw new RuntimeException("Unimplemented");    }    public static function getFoo(){        return static::foo();    }}class ConcreteFoo extends AbstractFoo{    public static function foo(){        return "bar";    }}echo ConcreteFoo::getFoo();

gives the expected "bar".

Note that this is not really polymorphism. The static keywork is just resolved into the class from which the static method was called. If you declare an abstract static method, you will receive a strict warning. PHP just copies all static methods from the parent (super) class if they do not exist in the child (sub) class.


You notice that word self?

That is pointing to AbstractClass. Thus it is calling AbstractClass::foo(), not ConcreteClass::foo();

I believe PHP 5.3 will provide late static bindings, but if you are not on that version, self will not refer to an extended class, but the class that the function is located in.

See: http://us.php.net/manual/en/function.get-called-class.php


It's a rule that abstract and static keywords can not be use on a method at the same time.

A method with an abstract keyword means that sub-class must implement it. Adding static to a method of a class allows us to use the method without instantiating it.

So that is why the error occurs.