tags:

views:

29

answers:

2

Let's have this class

class A {
  protected static function ident() { return "I am A"; }
  public static function say() { return "I say '".self::ident()."'!"; }
}

Then I need to extend class A and override the ident() like this

class B extends A {
  protected static function ident() { return "I am B"; }
}

Now when B::say(); is called, the result is I say 'I am A'. Is there any technique how to force it to produce I say 'I am B' without overriding the say() method? (Please don't ask me why to do this, just trust me it is reasonable in my project)

I believe it is possible via abstract class or interface, but I can not figure out how. If it is impossible in PHP, is there any language (except Haskell) which implements this feature?

A: 

say() is a static method, this kind of method belong to the class, not to an instance, it's not really inherited. If you want to create your own method you have to "override it" (but again it's not overriding).

Colin Hebert
+1  A: 

Since PHP 5.3 late static bindings are available. You shouled take a look at that.

http://php.net/manual/en/language.oop5.late-static-bindings.php

Mchl
Bingo! This solves the problem! Thank you!public static function say() { return "I say '".static::ident()."'!"; }
Jan Turoň