Jan Turoň
Jan Turoň

Reputation: 32912

general inheritance (not just) in PHP

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?

Upvotes: 1

Views: 58

Answers (2)

Mchl
Mchl

Reputation: 62369

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

Upvotes: 1

Colin Hebert
Colin Hebert

Reputation: 93157

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).

Upvotes: 0

Related Questions