On 17.08.2010, at 10:46, Ferenc Kovacs wrote:

> Tue, Aug 17, 2010 at 10:04 AM, Richard Quadling <rquadl...@gmail.com>wrote:
> 
>> On 17 August 2010 08:39, Jingcheng Zhang <dio...@gmail.com> wrote:
>>> Hello internals,
>>> 
>>> I wonder whether it is possible to implement "static initialization
>> block"
>>> feature in PHP, for example:
>>> 
>>> <?php
>>> class Foo {
>>> 
>>> }
>>> class Bar {
>>>   public static $baz = 'baz';
>>>   public static $foo;
>>>   static {
>>>       // After loading this file, self::$foo is initialized as a Foo
>>> instance.
>>>       self::$foo = new Foo();
>>>   }
>>> }
>>> ?>
>>> 
>>> Currently we have to do this outside the class definition as static
>> variable
>>> initialization is only limited to constant values.
>>> However in some circumstance, "dynamic" initialization of static variable
>> is
>>> expected and meaningful.
>>> 
>>> Thanks in advance!
>>> 
>>> --
>>> Best regards,
>>> Jingcheng Zhang
>>> P.R.China
>>> 
>> 
>> Implementing a singleton method is a common solution and one that is
>> well understood and documented.
>> 
>> Another option would be to put the initialisation immediately after
>> the class definition, so once the class is loaded, a static instance
>> is prepared. This saves the consumer of the class from having to do 1
>> additional line of code.
>> 
>> Or you could load the public static methods with a JIT call to prepare
>> the static instance. Overhead is that every call will have to test
>> self::$instance
>> 
>> 
>> There is probably some argument against "statics" being "dynamic"
>> though ... not my area of expertise to argue either way.
>> 
>> --
>> Richard Quadling.
>> 
>> --
>> PHP Internals - PHP Runtime Development Mailing List
>> To unsubscribe, visit: http://www.php.net/unsub.php
>> 
>> 
> This was brought up in the past:
> http://www.mail-archive.com/internals@lists.php.net/msg46458.html
> I still think that it's weird, that I can define a constant to a dynamic
> value (eg. by a complex expression or a function's return value), but I
> can't do that with the class consts. and with 5.3, we have two different
> kind of consts, you can define constants in compile time with const where
> you can't use expressions, and you can use the define method, where you can
> use expressions.
> and you can combine them:
> 
> define("NOW", time());
> var_dump(NOW);
> const BAR = NOW;
> var_dump(BAR);
> class baz{
>  const BAR = NOW;
> }
> var_dump(baz::BAR);'
> 
> with that in mind, I think we could allow complex expression to the const:
> the expression will be stored as-is, and when it's referenced (JIT) then it
> will be evaluated.
> 
> and this could be used also for variables also:
> 
> class foo{
>  public $now = time();
> }
> $foo = new foo;
> echo $foo->now;
> 
> ps: I predict somebody will say: can of worms! :)
> 
> Tyrael

Can do: can of worms.

To the original poster: use a singleton or add the init declarations after the 
class declaration, works just fine with autoloads (I recommend the singleton 
approach).

- David


Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to