如何从对象内联使用多个函数? 我有简单的类(class):
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
最佳答案
您返回 $this
这样您就可以继续处理该对象:
class test
{
private $string;
function text($text)
{
$this->string = $text;
return $this;
}
function add($text)
{
$this->string .= ' ' . $text;
return $this;
}
}
关于php - 在类中使用超过 1 个内联函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27713628/