Why can’t I set a public member variable using a function?
<?
class TestClass {
public $thisWorks = "something";
public $currentDir = dirname( __FILE__ );
public function TestClass()
{
print $this->thisWorks . "\n";
print $this->currentDir . "\n";
}
}
$myClass = new TestClass();
?>
Running it yields:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in /tmp/tmp.php on line 7
You cannot have expressions in the variable declarations. You can only use constant values. The
dirname()may not appear in this position.If you were to use PHP 5.3 you could use:
Otherwise you will have to initialize
$this->currentDirin the__constructor.