I'm pretty new to PHP, but I've been programming in similar languages for years. I was flummoxed by the following:
class Foo { public $path = array( realpath(".") );}
It produced a syntax error: Parse error: syntax error, unexpected '(', expecting ')' in test.php on line 5
which is the realpath
call.
But this works fine:
$path = array( realpath("."));
After banging my head against this for a while, I was told you can't call functions in an attribute default; you have to do it in __construct
. My question is: why?! Is this a "feature" or sloppy implementation? What's the rationale?