Calling a static method from a class that inherits an interface

Possible duplicate:
Calling a static member of a subclass

Please read the following code to understand my problem.

<?php Interface IDoesSomething { public static function returnSomething(); } abstract class MiddleManClass implements IDoesSomething { public static function doSomething() { return 1337 * self::returnSomething(); } } class SomeClass extends MiddleManClass { public static function returnSomething() { return 999; } } // and now, the vicious call $foo = SomeClass::doSomething(); /** * results in a * PHP Fatal error: Cannot call abstract method IDoesSomething::returnSomething() */ ?> 

Is there a way to force the abstraction returnSomething() , while retaining the ability to call a function from a function defined in the abstract class "mediator"? Looks like a PHP bottleneck for me.

+4
source share
3 answers

If you php version> = 5.3, change

 public static function doSomething() { return 1337 * self::returnSomething(); } 

to

 public static function doSomething() { return 1337 * static::returnSomething(); } 
+9
source

Why are you using statics, is this not a very good OOP? Statics are not suitable for use in inheritance, since they are designed to provide functionality specifically for this class. It does what you need.

 <?php Interface IDoesSomething{ public function returnSomething(); } abstract class MiddleManClass implements IDoesSomething{ public function doSomething(){ return 1337 * $this->returnSomething(); } } class SomeClass extends MiddleManClass{ public function returnSomething(){ return 999; } } $someClass = new SomeClass(); $foo = $someClass->doSomething(); 
+2
source

This issue is known as late static binding: php manual

+2
source

Source: https://habr.com/ru/post/1392319/


All Articles