Patrick Schaaf wrote on 12/03/2015 18:40:

Am 12.03.2015 18:56 schrieb "Rowan Collins" <rowan.coll...@gmail.com <mailto:rowan.coll...@gmail.com>>:
>
> Johannes Ott wrote on 12/03/2015 17:05:
>
>>>> So doing a null check each time
>>>> is a overhead of calculation which can be avoided with this static
>>>> constructor pattern.
>>>
>>> Presumably the engine would need to perform some implicit equivalent of
>>> "if ( ! self::$initialised )" on each call to decide if the static
>>> constructor needs to be called or not, so the overhead is not completely
>>> eliminated.
>>>
>> Yes you are right but I think it can be done more efficiently inside the
>> interpreter using some struct flags then have to parse each time inside
>> a coded part in the application.
>
> Yes, point taken.

I don't think such a flag is neccessary at all. Any class. at the moment, comes from one or another file that is included / required. And all of the code in these files outside the class definition, is then immediately executed. The only thing neccessary would be to check, just before that execution begins, which of the new classes have such an initializer method, and then call that, before the execution of the file itself begins.


This was my initial interpretation, but Johannes has explained that that is not the intention of this proposal. Instead, it is intended to be called on first *use* of the class; a subtle difference, but given this code:

class A { public static $foo; private function __static() { echo 'A'; } }
class B { public static $foo; private function __static() { echo 'B'; } }
B::$foo = 1;
A::$foo = 2;

Running the magic at definition time will echo 'A' then 'B'; running it on first use will echo 'B' then 'A'.


Incidentally that is something that cannot be emulated with the autoloader-does-it approach, because the autoloader can only do that after the include/require is complete - i.e. code within the file will not yet see the class as initialized (in that approach).

For that scenario, the autoloader can immediately call $class_name::__static() or whatever. The only edge-case is when you try to also put loose code in your class definition files, but why would you need to?

Regards,
--
Rowan Collins
[IMSoP]

Reply via email to