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:
public $currentDir = __DIR__ ;
Otherwise you will have to initialize $this->currentDir
in the __construct
or.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With