1

When using chained functions, is there a way to determine if the current call is the last in the chain?

For example:

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

I want to check in first, second and third if the call is the last in the chain so it saves me to write a result-like function to always add to the chain. In this example the check should result true in third.

4

3 回答 3

3

No, not in any way that's sane or maintainable.
You'll have to add a done() method or similar.

于 2012-06-13T13:40:44.790 回答
3

As far as i know it's impossible, i'd suggest to use finishing method like this:

$oObject->first()
  ->second()
  ->third()
  ->end(); // like this
于 2012-06-13T13:41:44.140 回答
1

If you want to execute a function on the last chain (without addional exec or done on the chain).

The code below will obtain the full chain from the source code and return the data after the last chain.

<?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;
    }
}
于 2018-08-12T02:47:58.810 回答