链式操作(fluent interface)

一个常见的设计模式

fluent interface(流利接口)有一个更广为人知的名字『链式操作』,可能大多数人大概都是从Jquery最先熟悉的,在laravel中,ORM的一系列sql操作,也是链式操作,特点是每次都返回一个Query Builder对象。

实现

class Employee
{
public $name;
public $surName;
public $salary;
public function setName($name)
{
$this->name = $name;
return $this;
}
public function setSurname($surname)
{
$this->surName = $surname;
return $this;
}
public function setSalary($salary)
{
$this->salary = $salary;
return $this;
}
public function __toString()
{
$employeeInfo = 'Name: ' . $this->name . PHP_EOL;
$employeeInfo .= 'Surname: ' . $this->surName . PHP_EOL;
$employeeInfo .= 'Salary: ' . $this->salary . PHP_EOL;
return $employeeInfo;
}
}
//链式操作的效果
$employee = (new Employee())
->setName('Tom')
->setSurname('Smith')
->setSalary('100');
echo $employee;
# 输出结果
# Name: Tom
# Surname: Smith
# Salary: 100
这里面能够连续链式操作的关键就在于 每个方法 都返回 return $this;
tags: PHP,设计模式