【发布时间】:2011-05-27 17:54:09
【问题描述】:
为什么我不能使用函数设置公共成员变量?
<?
class TestClass {
public $thisWorks = "something";
public $currentDir = dirname( __FILE__ );
public function TestClass()
{
print $this->thisWorks . "\n";
print $this->currentDir . "\n";
}
}
$myClass = new TestClass();
?>
运行它会产生:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in /tmp/tmp.php on line 7
【问题讨论】:
-
变量声明中不能有表达式。您只能使用常量值。
-
@mario:这是一个答案,不是评论。
-
@delnan:只是我喜欢避免使用单行字作为答案;但现在制作了一些填充文本..