Re: static constructor
| From: | Patrick Schaaf | Date: | Thu, 12 Mar 2015 08:14:56 +0000 |
| Subject: | Re: static constructor | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-84612@lists.php.net to get a copy of this message | ||
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