使用链式函数时,有没有办法确定当前调用是否是链中的最后一个?
例如:
$oObject->first()->second()->third();
我想检查first
,second
和third
,如果该调用是链中的最后一个,那么它可以节省我写一个result
- 类似的功能总是添加到链。在此示例中,检查应在third
中生效。
答案 0 :(得分:3)
不,不以任何方式理智或可维护
您必须添加done()
方法或类似方法。
答案 1 :(得分:3)
据我所知,这是不可能的,我建议使用这样的整理方法:
$oObject->first()
->second()
->third()
->end(); // liek this
答案 2 :(得分:1)
如果要在最后一个链上执行功能(没有附加的exec或在链上完成)。
下面的代码将从源代码中获取完整链,并在最后一个链之后返回数据。
<?php
$abc = new Methods;
echo($abc->minus(12)->plus(32)); // output: -12+32
echo(
$abc->plus(84)
->minus(63)
); // output: +84-63
class Methods{
private $data = '';
private $chains = false;
public function minus($val){
$this->data .= '-'.$val;
return $this->exec('minus');
}
public function plus($val){
$this->data .= '+'.$val;
return $this->exec('plus');
}
private function exec($from){
// Check if this is the first chain
if($this->chains === false){
$this->getChains();
}
// Remove the first chain as it's
// already being called
if($this->chains[0] === $from){
array_shift($this->chains);
}
else
die("Can't parse your chain");
// Check if this is the last chain
if(count($this->chains) === 0){
$copy = $this->data;
// Clear data
$this->chains = false;
$this->data = '';
return $copy;
}
// If not then continue the chain
return $this;
}
private function getChains(){
$temp = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
// Find out who called the function
for ($i=0; $i < count($temp); $i++) {
if($temp[$i]['function'] === 'exec'){
$temp = $temp[$i + 1];
break;
}
}
// Prepare variable
$obtained = '';
$current = 1;
// Open that source and find the chain
$handle = fopen($temp['file'], "r");
if(!$handle) return false;
while(($text = fgets($handle)) !== false){
if($current >= $temp['line']){
$obtained .= $text;
// Find break
if(strrpos($text, ';') !== false)
break;
}
$current++;
}
fclose($handle);
preg_match_all('/>(\w.*?)\(/', $obtained, $matches);
$this->chains = $matches[1];
return true;
}
}