开发者

What does "return $this" mean?

I'm trying to understand this code, and when I arrived at the final line, I didn't get it. :(

Can I have your help in order to find out, what does return $this mean ?

public function setOptions(array $options) {
    $methods = get开发者_运维问答_class_methods($this);
    foreach ($options as $key => $value) {
        $method = 'set' . ucfirst($key);
        if (in_array($method, $methods)) {
            $this->$method($value);
        }
    }

    //???? - return what ?
    return $this;
}

Update:

I've removed my comments for better clarification.


This way of coding is called fluent interface. return $this returns the current object, so you can write code like this:

$object
  ->function1()
  ->function2()
  ->function3()
  ;

instead of:

$object->function1();
$object->function2();
$object->function3();


This will return the instance this method is called on. This usually done for achieving fluent interfaces so you can call stuff like:

CoolClass::factory('hello')->setOptions(array('coolness' => 5))->sayHello();

Where both setOptions and sayHello would be called on the same object.


$this means the current object, the one the method is currently being run on. By returning $this a reference to the object the method is working gets sent back to the calling function.

So anyone doing

 $foo2 = $foo->SetOptions($bar);

$foo2 now refers to $foo also.


you just can create a function chain

class My_class
{

        public function method1($param)
        {
                /*
                 * logic here
                 */

                return $this;
        }

        public function method2($param)
        {
                /*
                 * logic here
                 */

                return $this;
        }

        public function method3($param)
        {
                /*
                 * logic here
                 */

                return $this;
        }

}

so you can use this

            My_class obj = new My_class();

            $return = obj->method1($param)->method2($param)->method3($param);


$this would be the class that contains that function.

So if you were to call it like:

$obj->setOptions($options)

it's going to return $obj, which has been set with the new options. Generally when something is set like this, you don't have to capture the return, because it's affecting the object itself, but it makes it so you can use it inline.


If the SetOptions method is part of a ProgramOptions class or something, $this would refer to the class containing the method, so you would be passing back an instance of ProgramOptions.


Its a common OOP technique called Fluent Interface. It main purpose to help chain multiple method calls in languages, that do not support method cascading, like PHP. So

return $this;

will return an updated instance(object) of that class so it can make another call in its scope. See example in PHP,

class Class_Name {
    private field1;
    private field2;
    private field3;

    public function setField1($value){

        $this->field1 = $value;

        return $this; 
    }

    public function setField2($value){

        $this->field2 = $value;

        return $this; 
    }

    public function setField3($value){

        $this->field3 = $value;

        return $this; 
    }
} 

$object = new Class_Name();
$object->setField1($value1)->setField2($value2)->setField3($value3);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜