Reputation: 53607
(PHP7) 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(){
self::$b();
self::$b=function(){};
}
}
a::$b=function(){echo 'Here I do very heavy stuff, but will happen only in the first time I call init()';};
for($i=0;$i<1000;$i++){
a::init();
}
In php7 it will give an error that it expects a::$b
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?
Upvotes: 4
Views: 63
Reputation: 39434
You can either use PHP 7 Uniform Variable Syntax:
(self::$b)();
Or a temporary variable in PHP 5+ (including 7):
$init = self::$b;
$init();
Upvotes: 5