Reputation: 14866
Is it possible to chain properties together in PHP?
I tried to get it working like with method calls, similar to this:
class DefClass
{
private $_definitions = array();
public function __set($name, $value)
{
$this->_definitions[$name] = $value;
return $this;
}
}
$test = new DefClass();
$test
->foo = 'bar'
->here = 'there'
->goodbye = 'hello';
But it didn't work. Is it only possible to return the object and access it again with a method call?
Upvotes: 1
Views: 54
Reputation: 31614
That's not even proper syntax. Remember, overloading is not a normal function call (hence why it's refered to as magic). If you really want to do this, make it a true function and forgo the overloading
public function setVal($name, $value)
{
$this->_definitions[$name] = $value;
return $this;
}
And then you can do
$class->setVal('foo', 'bar')->setVal('bob', 'baz');
Upvotes: 2