base class:
abstract class Challenge
{
abstract **static** public function getName();
}
now two classes from it:
class ChallengeType1 extends Challenge
{
public **static** function getName()
{
return 'Swimming';
}
}
class ChallengeType2 extends Challenge
{
public **static** function getName()
{
return 'Climbing';
}
}
as you all might know, we can’t use static, but it would be reasonable. So I can’t do like that: for example, I have the classname, so I want to know it’s name: ChallengeType2::getName(); – it will fail! First I should construct the object – looks unnecessary (not to mention, what it this class have very complex initialization?)
Turns out you cannot have
static abstractmethods on anabstract class. See here:Why does PHP 5.2+ disallow abstract static class methods?
But you can declare an
interfaceto require astaticmethod.Here is an example that compiles:
Working Example