Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've set up an abstract parent class, and a concrete class which extends it. Why can the parent class not call the abstract function?

//foo.php <?php     abstract class AbstractFoo{         abstract public static function foo();         public static function getFoo(){             return self::foo();//line 5         }     }      class ConcreteFoo extends AbstractFoo{         public static function foo(){             return "bar";         }     }      echo ConcreteFoo::getFoo(); ?> 

Error:

Fatal error: Cannot call abstract method AbstractFoo::foo() in foo.php on line 5

like image 295
Cam Avatar asked May 18 '10 17:05

Cam


People also ask

Can we call function in abstract class?

You can call only static methods of an abstract class (since an instance is not required).

Can abstract class have function definition PHP?

An abstract method can not contain body: Methods defined as abstract simply declare the method's signature - they cannot define the implementation.

Can abstract class inherit another abstract class PHP?

Yes you can inherit abstract class from another abstract class.

Can abstract class have non abstract methods PHP?

An abstract class can contain abstract as well as non abstract methods.


2 Answers

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.

like image 99
Artefacto Avatar answered Oct 11 '22 06:10

Artefacto


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

like image 38
Tyler Carter Avatar answered Oct 11 '22 05:10

Tyler Carter