PHP链式方法,如何知道最后一个

The situation:

Class MyClass {
    ...
    public function method($args) {
        // code
        ...
        if (is the last call) {
            return $something;
        }
        return $this;
    }
    ...
}
....
$obj = new MyClass;
$obj->method($some_args)->method($something_else)->method($more_data);

How can I know if the last call of method is actually the last?

When a function is called it is at that moment ALWAYS the last call to that function. PHP doesn't know if your script will actually do another function call after that one.

That being said you could probably bypass using the __destruct magic function, for example:

<?php

Class MyClass {
    private $method_queue = array();

    public function method($args) {
        array_push($this->method_queue, $args);
        return $this;
    }

    private function _method($args, $is_last) {
        // do actual stuff
        echo $args;

        if ($is_last) {
            echo "LAST";
            // do more stuff
        }
    }

    public function __destruct()
    {
        foreach ($this->method_queue as $k=>$args) {
            $this->_method($args, count($this->method_queue)-1==$k);
        }
    }
}

$obj = new MyClass;
$obj->method(1)->method(2)->method(3);