What does “return $this” mean?

主宰稳场 提交于 2020-05-07 10:04:19

问题


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.


回答1:


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();



回答2:


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.




回答3:


$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.




回答4:


$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.




回答5:


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);



回答6:


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.




回答7:


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);


来源:https://stackoverflow.com/questions/5956999/what-does-return-this-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!