如何在PHP5中链接对象:$ this-> foo-> bar-> baz() [英] Howto chain objects in PHP5: $this->foo->bar->baz()
本文介绍了如何在PHP5中链接对象:$ this-> foo-> bar-> baz()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在PHP5类中创建链接对象?示例:
How do I make chained objects in PHP5 classes? Examples:
$myclass->foo->bar->baz();
$this->foo->bar->baz();
Not: $myclass->foo()->bar()->baz();
另请参阅:
http://www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html < a>
See also:
http://www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html
推荐答案
只要你的$ myclass有一个成员/属性是一个实例本身,它就会像这样工作。
As long as your $myclass has a member/property that is an instance itself it will work just like that.
class foo {
public $bar;
}
class bar {
public function hello() {
return "hello world";
}
}
$myclass = new foo();
$myclass->bar = new bar();
print $myclass->bar->hello();
这篇关于如何在PHP5中链接对象:$ this-> foo-> bar-> baz()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文