# Static initialization block support in Class?

- Thread: https://externals.io/message/49435
- Original archive: https://news-web.php.net/php.internals/49435
- Started by Jingcheng Zhang on 2010-08-17T07:39:25+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #49435 — Jingcheng Zhang — 2010-08-17T07:39:25+00:00

URL: https://externals.io/message/49435#49435

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

---

## #49436 — Richard Quadling — 2010-08-17T08:04:26+00:00

URL: https://externals.io/message/49435#49436 · In reply to: #49435

> 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.

---

## #49437 — Ferenc Kovacs — 2010-08-17T08:46:04+00:00

URL: https://externals.io/message/49435#49437 · In reply to: #49436

Tue, Aug 17, 2010 at 10:04 AM, Richard Quadling <rquadling@gmail.com>wrote:

> 
> > […]
> block"
> > […]
> variable
> > […]
> is
> > […]
>
> 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.
>
> --
> 
> 
>
>
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

---

## #49438 — david.zuelke@bitextender.com — 2010-08-17T13:15:12+00:00

URL: https://externals.io/message/49435#49438 · In reply to: #49437

> Tue, Aug 17, 2010 at 10:04 AM, Richard Quadling <rquadling@gmail.com>wrote:
> 
> > […]
> 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

---

## #49440 — johannes@schlueters.de — 2010-08-17T14:58:48+00:00

URL: https://externals.io/message/49435#49440 · In reply to: #49437

Hi,


> > […]

As PHP allows code in the global scope I don't think this feature is
really needed. Especially as it has some potential conflicts when
defining the time by which it should be executed. You need all class
binding to be done when this static method is run, but in some cases we
delay the binding, so this won't be called when the class file is loaded
or we'd change some logic there. Simply using the global scope has an
already defined behavior.

> 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.

Well, the last time this was discussed I provided some pointers which
allow implementing this. The big issue is storing the code which shall
be executed.
The current way is that we store a name of a constant and resolve it
once it is needed the first time.
The most simple thing one might do, to extend this, is is to store an
OpArray for each constant. Depending on the changes done to the parser
this allows arbitrary statements with very little actual work needed,
but that's really messy in the result.
With this it can be really tricky to handle some errors, like recursion.
As long as we do a simple lookup this can be discovered quite easily.
("Fatal error:  Cannot declare self-referencing constant") With
arbitrary expressions you could trigger recursion in a way the engine
can't handle easily and where a user won't expect it (echo Foo::BAR; -
who would expect this to cause recursion?)

But well, feel free to implement it and play with it, maybe this all is
better than I think ... given the pointers from my previous mail and
some basic knowledge of the engine it should not take more than a day to
write a simple patch which allows experimenting.

johannes
