On Thursday 12 March 2015 00:21:34 Johannes Ott wrote:
>
> 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.

We are doing this in our private codebase, implemented as a feature of our 
autoloader: after having loaded a class, check whether it has a static method 
called 'init§class()', and call that.

At the moment, 63 of our 397 classes have that, so it's definitely a success 
for us.

One issue to think about, is whether to call that function when it's just 
inherited. We do, and in several places, then check if (get_called_class() == 
__CLASS__). It's a bit tedious in places, but in other places it permits 
setting up of subclass static::$properties from a shared baseclass initializer 
method.

In principle none of that is neccessary, because one can put code outside the 
class definition. One more line to write. In practise, it's a bit annoying, as 
it must go after the whole class definition, or it breaks for subclasses in 
funny ways when the parent class is not already defined...

Something like your version 2 proposal, a new __magic method, would be my 
prefered choice, too.

best regards
  Patrick

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to