Itay Moav -Malimovka Itay Moav -Malimovka - 1 month ago 4x
PHP Question

making a class's static-member a function (closure) does not work

Consider the following code, which tries to assign a function to a variable, and then make sure it is called only once.

class a{
static public $b;
static public function init(){
a::$b=function(){echo 'Here I do very heavy stuff, but will happen only in the first time I call init()';};


In php7 it will give an error that it expects
to be a string (the function name to call).

If I use pure variables and not static members, it will work.

My question, is this suppose to work, or not, or is there a small tweak I can do for this to work without pure vars?


You can either use PHP 7 Uniform Variable Syntax:


Or a temporary variable in PHP 5+ (including 7):

$init = self::$b;

As seen on