Re: abstract static function
| From: | Jingcheng Zhang | Date: | Thu, 05 Apr 2007 03:38:34 +0000 |
| Subject: | Re: abstract static function | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-28679@lists.php.net to get a copy of this message | ||
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,-- Best regards, Jingcheng Zhang Room 304, Dormitory 26 of Yuquan Campus, Zhejiang University P.R.ChinaI used to use abstract static function prior to php 5.2, because whenI 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 declaringstatic 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 notallow 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