Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static in php behaves strangly, can't accept functions

Tags:

php

php-5.3

This code throws parse error, which I do not understand why.

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

The question is, why?

like image 595
Itay Moav -Malimovka Avatar asked Dec 04 '22 05:12

Itay Moav -Malimovka


1 Answers

Quoting from the manual:

Note:

Trying to assign values to these [static] variables which are the result of expressions will cause a parse error.

(my emphasis)

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

like image 156
Mark Baker Avatar answered Dec 21 '22 01:12

Mark Baker