PHP: polymorphic abstract static methods
I'm trying to do something like this but i don't succeed.
开发者_C百科abstract class Animal
{
abstract static function getName();
static function sayName() { echo self::getName(); }
}
thanks!
You have two problems:
- static functions can't be abstact in php anymore.
- As said before, late static binding: as method getName() is defined in child class, you need to access it with static::getName() instead of self::getName()
It would have been nice if you'd have given a hint as to how you "don't succeed", but I suppose you're stumbling across static bindings and need to use late static bindings introduced in PHP 5.3.
My guess is maybe you are trying to instantiate an object from that class.
You can't. It is an abstract
class. Subclass it, and then instantiate that.
That will not succeed- you cannot have an abstract static function. See the accepted answer Why does PHP 5.2+ disallow abstract static class methods? for details on why.
精彩评论