【问题标题】:PHP - determine last function-call from chained function-callsPHP - 从链式函数调用中确定最后一个函数调用
【发布时间】:2012-06-13 13:36:31
【问题描述】:

使用链式函数时,有没有办法确定当前调用是否是链中的最后一个?

例如:

$oObject->first()->second()->third();

如果调用是链中的最后一个,我想检查firstsecondthird,这样我就不用编写类似result 的函数来始终添加到链中。在此示例中,检查结果应为 true,third

【问题讨论】:

  • 所有方法都返回对象本身吗?只有在他们这样做的情况下,才能以这种方式链接。至于您的问题,没有直接的方法可以实现您正在寻找的东西。有人可能会发布某种解决方法
  • 这怎么可能?当您链接功能时,您在第一个被取消堆叠时调用第二个。第二个函数如何知道它之前或之后是否有一个函数?
  • @Artragis,这正是我想知道的;)
  • @xbonex;是的,我使用了stackoverflow.com/questions/125268/… 作为参考。我的问题被简化了,但我在静态位置使用它来处理查询、排序、限制等。

标签: php method-chaining


【解决方案1】:

据我所知这是不可能的,我建议使用这样的整理方法:

$oObject->first()
  ->second()
  ->third()
  ->end(); // like this

【讨论】:

  • 是的,这就是我提到的类似结果的功能;)感谢您的回复。不幸听到这是不可能的,也许 PHP 的人为此目的设计了一些东西 ;)
【解决方案2】:

不,不是以任何理智或可维护的方式。
您必须添加 done() 方法或类似方法。

【讨论】:

    【解决方案3】:

    如果你想在最后一个链上执行一个函数(没有额外的 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-19
      • 2015-07-09
      相关资源
      最近更新 更多