Re: static constructor
| From: | Alexey Zakhlestin | Date: | Thu, 12 Mar 2015 08:24:18 +0000 |
| Subject: | Re: static constructor | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-84613@lists.php.net to get a copy of this message | ||
> On 12 Mar 2015, at 02:21, Johannes Ott <mail@deroetzi.de> wrote:
>
> So now I want to do my first own proposal for a new function in PHP and
> I hope doing it right with starting a discussion here first.
>
> The purpose of this suggestion is to introduce a static constructor,
> which is called before the first call to class either static or
> non-static to initialize some static properties which are needed by the
> class.
>
> I think about two different possible syntax to realize that purpose:
>
> Version 1 (similar to Java):
>
> class A {
> private static $var;
>
> static {
> //Do some code here to initialize self::$var;
> }
>
> }
>
> Version 2 (a new magic method):
>
> class B {
> private static $var;
>
> private static function __static() {
> //Do some code here to initialize self::$var;
> }
> }
>
> My prefered code would be version 2 at the moment.
>
> Looking forward to your feedback,
What about inheritance?
I think dynamic class-constructor would make much more sense.
A function which can analyse real class and do initialisation.
class A
{
protected static function __class_construct()
{
echo get_called_class().” class is defined\n";
}
}
class B extends A
{
}
#### output ####
A class is defined
B class is defined
--
Alexey Zakhlestin
https://github.com/indeyets
PGP key: http://indeyets.ru/alexey.zakhlestin.pgp.asc