PHP续函数[重复]

This question already has an answer here:

Hi how can I make a class object with the possibility of doing this:

<?php
    $someClass = new SomeClass;
    $sum = $someClass->addValues(1,22)->sumValues();
    echo $sum; // to give me 23
?>

Sorry for asking! This is what I meant and I just got the idea, so - Sorry for the Post.

<?php 
    class SomeClass {
        private $values = array();
        public function addValue(){
            $this->values = func_get_args();
            return $this;
        }

        public function getSum(){
            $sum = array_sum($this->values);
            return $sum;
        }

    }

    $SomeClass = new SomeClass;
    $result = $SomeClass->addValue(1,22,44,51)->getSum();
    echo $result;
?>

And for all of those who answered - Sorry but this was only an EXAMPLE, so I asked How to do it to help me not to argue with me is it an overkill or not. I needed the way to do it. Not like I will use the same code.

</div>

If you just want to sum a list of numbers then it can easily be done like this:

$sum = array_sum(array(1, 22));

If you are trying to learn how to use classes/objects then you can implement that like this:

class SomeClass {
    protected $values = array();
    public function addValues() {
        $this->values = array_merge($this->values, func_get_args());
        return $this;
    }
    public function sum() {
        return array_sum($this->values);
    }
}
$someClass = new SomeClass;
echo $someClass->addValues(1, 22)->sum(); // 23

The term you are finding is "Method chaining", see this link on wikipedia

Basically you need to write a method that returns $this

<?php
    class Foo{
        protected $message;
        function a($foo){
            $this->message .= $foo;
            return $this;
        }
        function b($foo){
            $this->message .= $foo;
            return $this;
        }
        function print_message(){
            echo $this->message;
        }
    }

    $foo = new Foo()
    $foo->a("Hello")->b("World");
    $foo->print_message();
    //output: 'HelloWorld'

?>