标签:
链式操作的格式是:$obj->(方法或属性)->(方法或属性)->(方法或属性)...方法或属性一般都是类中定义的函数。下面是一个事例:
<html> <head> <meta charset="utf-8"> <title></title> </head> <body> <?php class Student{ private $name; private $age; public function setName($name){ $this->name=$name; return $this; //一定要返回$this } public function setAge($age){ $this->age=$age; return $this; } public function getName(){ return $this->name; } public function getAge(){ return $this->age; } } $stu=new Student(); echo $stu->setName("Jane")->setAge(20)->getAge(); //输出的时候只能输出一个值 class Person{ public $stu; public function setStu(){ $this->stu=new Student(); return $this; } public function getStu(){ return $this->stu; } } $per=new Person(); echo $per->setStu()->getStu()->setName("Jenny")->setAge(18)->getAge(); //必须先setStu(),然后必须要getStu()后才能后续的函数 ?> </body> </html>
标签:
原文地址:http://www.cnblogs.com/-beyond/p/5694720.html