ngreenwood6 Posted October 16, 2010 Share Posted October 16, 2010 This is a bit of a more advanced question so I am hoping there are some advanced programmers online at this time . Anyways basically I want to be able to "queue" functions from a class in a single call and am wondering if I am doing it correctly or if there is a better way to do it. Here is my code: <?php class test { private static $one; private static $two; private static $three; public function callOne($val){ $one .= $val; return $this; } public function callTwo($val){ $two .= $val; return $this; } public function callThree($val){ $three .= $val; return $this; } public function print(){ echo $this->one.' '.$this->two.' '.$this->three; } } ?> now when I want to call this I can do: $test = new test(); $test->callOne('one')->callTwo('two')->callThree('three'); $test->callOne('another one'); $test->print(); Is there a better way to do this or is there a different method of doing this? Just wanna make sure I am doing it right lol. Quote Link to comment https://forums.phpfreaks.com/topic/215990-calling-multiple-functions-in-single-call-from-class/ Share on other sites More sharing options...
hitman6003 Posted October 16, 2010 Share Posted October 16, 2010 The only way to chain method invocations is to return the object from the previous call. http://www.electrictoolbox.com/php-method-chaining/ Quote Link to comment https://forums.phpfreaks.com/topic/215990-calling-multiple-functions-in-single-call-from-class/#findComment-1122662 Share on other sites More sharing options...
ngreenwood6 Posted October 16, 2010 Author Share Posted October 16, 2010 Thanks so basically I am doing it right. Kinda just discovered that on my own because I didnt know what to google but now I do "method chaining". Is this efficient? I assume so because $this should be passed as a reference correct? Quote Link to comment https://forums.phpfreaks.com/topic/215990-calling-multiple-functions-in-single-call-from-class/#findComment-1122664 Share on other sites More sharing options...
gizmola Posted October 16, 2010 Share Posted October 16, 2010 Yes the original name for this is a "fluent" interface. Your code is exactly what's needed to implement a fluent interface with php. Quote Link to comment https://forums.phpfreaks.com/topic/215990-calling-multiple-functions-in-single-call-from-class/#findComment-1122677 Share on other sites More sharing options...
ignace Posted October 16, 2010 Share Posted October 16, 2010 We had actually could of have helped you better if you instead of Test had put up a real example of your code. I would create a Façade (or a Builder if you are actually building something in steps) if those methods needs to be called in sequence. Because you are familiar with those functions and how they are called, you can't assume someone else will if they join your project. Quote Link to comment https://forums.phpfreaks.com/topic/215990-calling-multiple-functions-in-single-call-from-class/#findComment-1122682 Share on other sites More sharing options...
ngreenwood6 Posted October 16, 2010 Author Share Posted October 16, 2010 Thanks for the replies guys. I am actually setting up these functions to be a string builder of sort. They do not need to be called in order, when the "execute" function is called it pieces it all together. Quote Link to comment https://forums.phpfreaks.com/topic/215990-calling-multiple-functions-in-single-call-from-class/#findComment-1122781 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.