# abstract static function

- Thread: https://externals.io/message/28678
- Original archive: https://news-web.php.net/php.internals/28678
- Started by Jingcheng Zhang on 2007-04-05T03:32:06+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #28678 — Jingcheng Zhang — 2007-04-05T03:32:06+00:00

URL: https://externals.io/message/28678#28678

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

---

## #28679 — Jingcheng Zhang — 2007-04-05T03:38:34+00:00

URL: https://externals.io/message/28678#28679 · In reply to: #28678

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

---

## #28692 — Jakob Buchgraber — 2007-04-05T16:36:11+00:00

URL: https://externals.io/message/28678#28692 · In reply to: #28678

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
