Class static initialization block

From: Date: Sat, 11 Sep 2021 23:28:02 +0000
Subject: Class static initialization block
Groups: php.internals 
Request: Send a blank email to internals+get-116031@lists.php.net to get a copy of this message
Hello! I would like to suggest a feature I saw being implemented in the V8 9.4 engine called "*class static initialization block*". https://v8.dev/blog/v8-release-94 In short, it is a method that is executed once when a class is imported and loaded, allowing for some more advanced initialization process to be done. class Test { static readonly Carbon $now; static () { self::$now = now(); } } Currently I can only do this in a very hacky way, usually by creating a public static method and calling it after class initialization. class Test { static Carbon $now; public static function init(): void { self::$now = now(); } } Test::init(); I think the way the V8 does is much more interesting. Another alternative would be to create a magic method like __initialize().

« previous php.internals (#116031) next »