在方法外使用dirname()初始化此公共类变量时出错 [英] Getting an error trying to initialize this public class variable using dirname() outside a method
本文介绍了在方法外使用dirname()初始化此公共类变量时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么不能使用函数设置公共成员变量?
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();
?>
运行它产生:
Parse error: syntax error, unexpected '(', expecting ',' or ';' in /tmp/tmp.php on line 7
推荐答案
您不能在变量声明中有表达式,只能使用常量值 dirname )
可能不会出现在此位置。
You cannot have expressions in the variable declarations. You can only use constant values. The dirname()
may not appear in this position.
如果您要使用PHP 5.3,您可以使用:
If you were to use PHP 5.3 you could use:
public $currentDir = __DIR__ ;
否则,您将必须在 __结构
或
这篇关于在方法外使用dirname()初始化此公共类变量时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文