milesap Posted July 19, 2010 Share Posted July 19, 2010 I've seen some people code with objects in PHP, but one thing that others do (that I'd like to do) is the ability to call multiple methods at once. For example, in Zend Framework it's possible to write the following: $variable->setMessage('Text')->addColor('blue')->toArray(); (just an example) Notice how you can call several methods at once without having to write $variable next to each method? I've tried to do similar stuff in my code but cannot achieve this effect. Does anyone know how to set this up or point me in the right direction? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/208180-php-objects-and-methods-question/ Share on other sites More sharing options...
bh Posted July 19, 2010 Share Posted July 19, 2010 Hi, Its a great question and exciting at all. Heres a simple example what im just done: class class_a extends class_b { public function func_a() { echo 'm'; return $this; } } class class_b { public function func_b() { echo 'n'; } } $a = new class_a(); $a->func_a()->func_b(); Quote Link to comment https://forums.phpfreaks.com/topic/208180-php-objects-and-methods-question/#findComment-1088140 Share on other sites More sharing options...
milesap Posted July 19, 2010 Author Share Posted July 19, 2010 Hi, Its a great question and exciting at all. Heres a simple example what im just done: class class_a extends class_b { public function func_a() { echo 'm'; return $this; } } class class_b { public function func_b() { echo 'n'; } } $a = new class_a(); $a->func_a()->func_b(); Excellent! So all subsequent methods must be extensions of the last method called, makes a lot of sense when trying to build easy to read, flexible code. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/208180-php-objects-and-methods-question/#findComment-1088153 Share on other sites More sharing options...
milesap Posted July 19, 2010 Author Share Posted July 19, 2010 works great Quote Link to comment https://forums.phpfreaks.com/topic/208180-php-objects-and-methods-question/#findComment-1088155 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.