What does “return $this” mean?

前端 未结 7 420
遇见更好的自我
遇见更好的自我 2020-12-13 00:34

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 re

相关标签:
7条回答
  • 2020-12-13 01:02

    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();
    
    0 讨论(0)
  • 2020-12-13 01:12

    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 讨论(0)
  • 2020-12-13 01:17

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

    0 讨论(0)
  • 2020-12-13 01:18

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

    0 讨论(0)
  • 2020-12-13 01:25

    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.

    0 讨论(0)
  • 2020-12-13 01:25

    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);
    
    0 讨论(0)
提交回复
热议问题