相关疑难解决方法(0)

PHP函数作为参数默认值

以下面的函数为例:

private function connect($method, $target = $this->_config->db()) {
    try {
        if (!($this->_pointer = @fopen($target, $method)))
            throw new Exception("Unable to connect to database");
    }  catch (Exception $e) {
            echo $e->getMessage();
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我将函数$this->_config->db()插入到参数中,$target因为它是默认值.我理解这不是正确的语法,只是想解释我的目标.

$this->_config->db() 是一个吸气功能.

现在我知道我可以使用匿名函数并$target稍后调用它,但我$target也想接受直接的字符串值.

我怎么能给它一个默认的返回值,$this->_config->db()并且仍然可以用字符串值覆盖它?

php oop function

5
推荐指数
1
解决办法
783
查看次数

标签 统计

function ×1

oop ×1

php ×1