abstract static function

php.internals

Jingcheng Zhang

19 years ago
Hi internals, I used to use abstract static function prior to php 5.2, because when I consider static class as a singleton, I can use abstract static function to delay the implementation of some methods to the derived classes. This happens, for example, when I declare a parent dispatcher, and extend it into two classes - action dispatcher and ajax dispatcher - and declare them static as I want to keep them a singleton because they are just application models, not domain models: <?php abstract class Dispatcher { abstract public static function dispatch(); public static function exampleMethod() { ... } } final class ActionDispatcher { public static function dispatch() { parent::exampleMethod(); // other code... } } final class AjaxDispatcher { public static function dispatch() { parent::exampleMethod(); // other code... } } ?> Unfortunately, since php 5.2, although it does still allow declaring static methods in an interface, it simply generates a message when declaring abstract static functions with error_reporting set to E_ALL | E_STRICT. PHP is a dynamic language, not static as C++ and Java. So, why not allow abstract static functions? It is also a good way to support singleton natively. Waiting for explanations. Thanks :)
-- Best regards, Jingcheng Zhang Room 304, Dormitory 26 of Yuquan Campus, Zhejiang University P.R.China

Jingcheng Zhang

19 years ago
Sorry, I've forgotten the "extends" keywords in the example codes: <?php abstract class Dispatcher { abstract public static function dispatch(); public static function exampleMethod() { ... } } final class ActionDispatcher extends Dispatcher { public static function dispatch() { parent::exampleMethod(); // other code... } } final class AjaxDispatcher extends Dispatcher { public static function dispatch() { parent::exampleMethod(); // other code... } } ?> 2007/4/5, Jingcheng Zhang <diogin@gmail.com>:
> > Hi internals, > I used to use abstract static function prior to php 5.2, because when > I consider static class as a singleton, I can use abstract static function > to delay the implementation of some methods to the derived classes. This > happens, for example, when I declare a parent dispatcher, and extend it into > two classes - action dispatcher and ajax dispatcher - and declare them > static as I want to keep them a singleton because they are just application > models, not domain models: > > <?php > abstract class Dispatcher { > abstract public static function dispatch(); > public static function exampleMethod() { > ... > } > } > final class ActionDispatcher { > public static function dispatch() { > parent::exampleMethod(); > // other code... > } > } > final class AjaxDispatcher { > public static function dispatch() { > parent::exampleMethod(); > // other code... > } > } > ?> > Unfortunately, since php 5.2, although it does still allow declaring > static methods in an interface, it simply generates a message when declaring > abstract static functions with error_reporting set to E_ALL | E_STRICT. > PHP is a dynamic language, not static as C++ and Java. So, why not > allow abstract static functions? It is also a good way to support singleton > natively. > Waiting for explanations. Thanks :) > > -- > Best regards, > Jingcheng Zhang > Room 304, Dormitory 26 of Yuquan Campus, Zhejiang University > P.R.China
-- Best regards, Jingcheng Zhang Room 304, Dormitory 26 of Yuquan Campus, Zhejiang University P.R.China

Jakob Buchgraber

19 years ago
Jingcheng Zhang wrote:
> Hi internals, > I used to use abstract static function prior to php 5.2, because when I > consider static class as a singleton, I can use abstract static function to > delay the implementation of some methods to the derived classes. This > happens, for example, when I declare a parent dispatcher, and extend it > into > two classes - action dispatcher and ajax dispatcher - and declare them > static as I want to keep them a singleton because they are just application > models, not domain models: > > <?php > abstract class Dispatcher { > abstract public static function dispatch(); > public static function exampleMethod() { > ... > } > } > final class ActionDispatcher { > public static function dispatch() { > parent::exampleMethod(); > // other code... > } > } > final class AjaxDispatcher { > public static function dispatch() { > parent::exampleMethod(); > // other code... > } > } > ?> > Unfortunately, since php 5.2, although it does still allow declaring > static methods in an interface, it simply generates a message when > declaring > abstract static functions with error_reporting set to E_ALL | E_STRICT. > PHP is a dynamic language, not static as C++ and Java. So, why not allow > abstract static functions? It is also a good way to support singleton > natively. > Waiting for explanations. Thanks :) >
Maybe because it doesn't make sense to declare an abstract static method as a static method always belongs to a class. So if you declare it as abstract it needs to be overridden and wouldn't belong to the class anymore as each subclass has it's own implementation. So from an OO point of view this would be a complete nonsense. Cheers, Jay