【发布时间】:2015-02-27 02:25:11
【问题描述】:
如何从对象内联使用超过 1 个函数? 我有简单的课程:
class test
{
private $string;
function text($text)
{
$this->string = $text;
}
function add($text)
{
$this->string .= ' ' . $text;
}
}
那么我如何使用这个类:
$class = new test();
$class->text('test')->add('test_add_1')->add('test_add_2');
不喜欢:
$class = new test();
$class->text('test')
$class->add('test_add_1')
$class->add('test_add_2')
在 $string 类的末尾将是:test test_add_1 test_add_2
【问题讨论】:
-
我相信你必须在函数末尾
return $this -
PHP method chaining?的可能重复
标签: php function class object optimization