PHP Method chaining Confusion

前端 未结 4 1811
情话喂你
情话喂你 2021-01-27 09:04

i have been lately introduced to method chaining, and am not sure if what I\'m doing here is illegal, or I\'m doing it wrong. I have a database Class like:

    c         


        
相关标签:
4条回答
  • 2021-01-27 09:10

    In query(), you need to return $this otherwise there's nothing to chain it with when the function returns

    public function query($query)
    {
       $this->last_query = $query;
       $this->resultset = mysql_query($query, $this->connection);
       return $this;  // <- here
    }
    
    0 讨论(0)
  • 2021-01-27 09:21

    If you want to using method chaining, what you should do is return $this.

    public function query($query)
    {
        $this->last_query = $query;
        $this->resultset = mysql_query($query, $this->connection);
        return $this;
    }
    

    Then you can do this:

    $db->query("SELECT * FROM users WHERE name='JimmyP'")->fetchObject();
    
    0 讨论(0)
  • 2021-01-27 09:25

    Method chaining works by returning an object from a function.

    $obj = someFunction();
    $obj->someMethod();
    

    someFunction returns an object which has a method someMethod, which you can call. Very simple stuff. You can write it like this, without explicitly storing the returned object in a variable:

    someFunction()->someMethod();
    

    The ->someMethod() simply works on whatever value someFunction returns.

    So to use method chaining, you need to return an object from your methods. An object can also return itself with return $this, so you can chain methods of the same object on itself.

    0 讨论(0)
  • 2021-01-27 09:31

    Introducing Method Chaining: To enable method chaining in our previous example, we need to add only a single line of code in each 'setXXX' function. And that code is return $this;. Now our class looks like:

    class Person
    {
        private $name;
        private $age;
    
        public function setName($Name)
        {
            $this->name = $Name;
            return $this;//Returns object of 'this' i.e Person class
        }
    
        public function setAge($Age)
        {
            $this->age = $Age;
            return $this;//Again returns object of 'this' i.e Person class
        }
    
        public function findMe()
        {
            echo "My name is ".$this->name." and I am ".$this->age. " years old.";
        }
    }
    

    Now lets access our class methods through method chaining:

    $myself = new Person();
    $myself->setName('Arvind Bhardwaj')->setAge('22')->findMe();
    

    Explanation of concept:

    Surely you're a bit confused about precisely what is going on here. Lets go through this code in an easy way. Before that remember that method chaining always works from left to right!

    $myself = new Person() creates a new object of the Person class, quite easy to guess though.

    Next, $myself->setName('Arvind Bhardwaj') assigns the name to a variable and returns the object of the same class.

    Now $myself->setName('Arvind Bhardwaj') has become an object of the Person class, so we can access the Person class by using $myself->setName('Arvind Bhardwaj') as an object.

    Now we set the age as $myself->setName('Arvind Bhardwaj')->setAge('22'). setAge() again returns the object of this class, so the complete phrase $myself->setName('Arvind Bhardwaj')->setAge('22') is now an object of Person.

    Finally we print the user information by accessing findMe method as:

    $myself->setName('Arvind Bhardwaj')->setAge('22')->findMe();
    
    0 讨论(0)
提交回复
热议问题