Itay Moav -Malimovka Itay Moav -Malimovka - 1 year ago 64
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?

Answer Source

You can either use PHP 7 Uniform Variable Syntax:


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

$init = self::$b;

As seen on

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download