Static in php behaves strangely, cannot accept functions

This code throws a parsing error that I don't understand why.

function t(){ return 'g'; } function l(){ static $b = t(); return $b; } l(); 

The question is why?

+4
source share
2 answers

static values ​​of the variables are filled in at the parsing stage, so they cannot contain inconsistent values.

You can implement value initialization using the following:

 function l(){ static $b; if (!$b) $b = t(); return $b; } 
+7
source

Quote from the manual:

Note:

Attempting to assign values ​​to these [static] variables that are the result of expressions will result in a parsing error.

(my emphasis)

cf http://www.php.net/manual/en/language.variables.scope.php Example # 7

+10
source

Source: https://habr.com/ru/post/1389956/


All Articles