开发者

How this class and sub methods use works?

I have been browsing some php source code and need to know how the following class and sub methods use works:

<?php
$me = new Person;
$me->name("Franky")-开发者_StackOverflow社区>surname("Chanyau")->phone("+22", "456 789");
?>

I have pretty solid knowledge of OOP so I don't want a 101. I just need to know how to make the above code possible.


Method chaining is possible, by

return $this;

at the end of the method.

Explained here: phpandstuff: Method Chaining Plus Magic Setters

These methods usually set an instance variable and then just return $this.

public function phone($param) {
  $this->phone = $param;
  return $this;
} 


methods name() surname() and phone() return an instance of Person. you can accomplish this by

return $this;

most probably these methods look like this:

public function name($name) {
    $this->name = $name;
    return $this;
}


like some others said, its a fluid interface http://en.wikipedia.org/wiki/Fluent_interface#PHP the Basic Idea is that a methof of a class always returns the object itself

class Car {
    private $speed;
    private $color;
    private $doors;

    public function setSpeed($speed){
        $this->speed = $speed;
        return $this;
    }

    public function setColor($color) {
        $this->color = $color;
        return $this;
    }

    public function setDoors($doors) {
        $this->doors = $doors;
        return $this;
    }
}

// Fluent interface
$myCar = new Car();
$myCar->setSpeed(100)->setColor('blue')->setDoors(5);

(via wiki)


It's called method chaining. Basically each class function returns the object itself ($this) so that the user can call more functions on the returned object.

public function name() {
    //other stuff...
    return $this;
}

http://www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html

http://www.electrictoolbox.com/php-method-chaining


The idea is if we return $this then we can chain the object method calls together. Here's the solution:

 <?php 

    class Person
    {
        private $strName;
        private $strSurname;
        private $ArrPhone = array();

        public function name($strName)
        {
            $this->strName = $strName;
            return $this; // returns $this i.e Person 
        }

        public function surname($strSurname)
        {
            $this->strSurname = $strSurname;
            return $this; // returns $this i.e Person
        }

        public function phone() 
        {   $this->ArrPhone = func_get_args(); //get arguments as array
            return $this; // returns $this i.e Person
        }

        public function __toString()
        {
            return $this->strName." ".$this->strSurname.", ".implode(" ",$this->ArrPhone);
        }
    }

    $me = new Person;
    echo $me->name("Franky")->surname("Chanyau")->phone("+22", "456 789");

?>


Correct answers, but to make the code work you should write:

$me = new Person();

instead of

$me = new Person;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜