为什么我不能使用函数设置公共成员变量?
<?
class TestClass {
public $thisWorks = "something";
public $currentDir = dirname( __FILE__ );
public function TestClass()
{
print $this->thisWorks . "\n";
print $this->currentDir . "\n";
}
}
$myClass = new TestClass();
?>
Run Code Online (Sandbox Code Playgroud)
运行它产生:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in /tmp/tmp.php on line 7
Run Code Online (Sandbox Code Playgroud)
mar*_*rio 13
您不能在变量声明中使用表达式.您只能使用常量值.该dirname()可能不会出现在这个位置.
如果您使用PHP 5.3,您可以使用:
public $currentDir = __DIR__ ;
Run Code Online (Sandbox Code Playgroud)
否则你将不得不$this->currentDir在__construct或初始化.
| 归档时间: |
|
| 查看次数: |
2292 次 |
| 最近记录: |