以下面的函数为例:
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()
并且仍然可以用字符串值覆盖它?