Reputation:
How can i use a more than 1 function inline from object? I have simple class:
class test
{
private $string;
function text($text)
{
$this->string = $text;
}
function add($text)
{
$this->string .= ' ' . $text;
}
}
so how i can use this class as:
$class = new test();
$class->text('test')->add('test_add_1')->add('test_add_2');
not like:
$class = new test();
$class->text('test')
$class->add('test_add_1')
$class->add('test_add_2')
And at end in class $string will be: test test_add_1 test_add_2
Upvotes: 3
Views: 38
Reputation: 9329
You return $this
so you can then continue work on the object:
class test
{
private $string;
function text($text)
{
$this->string = $text;
return $this;
}
function add($text)
{
$this->string .= ' ' . $text;
return $this;
}
}
Upvotes: 3