What does “return $this” mean?

前端 未结 7 455
遇见更好的自我
遇见更好的自我 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: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);
    

提交回复
热议问题