使参数默认值为CONSTANT = PHP中的字符串


make parameter default value a CONSTANT = a string in PHP

我试图创建函数参数的默认值,作为常量与字符串连接的组合,但似乎我不能在那里进行连接操作。

public function __construct($test = __DIR__."/mypath") { ...

当然,我可以让这个>

public function __construct($test = null) {
    if($test === null) {
        $test = __DIR__."/mypath"
    }

但是我想知道是否有更干净的方法。是吗?

创建一个类变量,然后重新赋值构造

var $path = __DIR__."/mypath"; // Really better off to be an absolute path
public function __construct($test = null) {
    if($test !== null) {
        $this->path = $test;
}