Home >php教程 >php手册 >PHP中其实也可以用方法链

PHP中其实也可以用方法链

WBOY
WBOYOriginal
2016-06-06 20:39:211203browse

javascript里有个很有效的用法即方法链,在PHP里也可以使用方法链

简单示意一下:
代码如下:
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