Home  >  Article  >  Backend Development  >  PHP中其实也可以用方法链_php技巧

PHP中其实也可以用方法链_php技巧

WBOY
WBOYOriginal
2016-05-17 09:14:381226browse

简单示意一下:

复制代码 代码如下:

class test {
private $_name = '';
public function setName($name)
{
$this->_name = $name;
return $this;
}
public function getName()
{
echo $this->_name . "\n";
return $this;
}
}
$link = new test();
// 方法链
$link->setName('name1')->getName()->setName('name2')->getName()->setName('name3')->getName();


结果如下:
复制代码 代码如下:

name1
name2
name3
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn