The Little Guy Posted April 26, 2011 Share Posted April 26, 2011 In javascript you can do multiple methods on the same line like: if(document.getElementById('myElement').className.match(/^[0-9]/)){/*Do something*/} in that we have getElementById() and match() on the same line, and it works the same as if you were to split them on multiple lines. Is it possible to do that with php? For example: $obj = new MyObject(); $obj->add(2, 3)->to_string(); Quote Link to comment https://forums.phpfreaks.com/topic/234790-multi-level-methods/ Share on other sites More sharing options...
AbraCadaver Posted April 26, 2011 Share Posted April 26, 2011 Yes, method chaining. So long as the methods return an object with the next method, normally return $this. Quote Link to comment https://forums.phpfreaks.com/topic/234790-multi-level-methods/#findComment-1206626 Share on other sites More sharing options...
The Little Guy Posted April 26, 2011 Author Share Posted April 26, 2011 do you need to do anything special to do it? Quote Link to comment https://forums.phpfreaks.com/topic/234790-multi-level-methods/#findComment-1206627 Share on other sites More sharing options...
KevinM1 Posted April 26, 2011 Share Posted April 26, 2011 do you need to do anything special to do it? Like AC said, each method has to return an object for the next method to act upon. Typically, it's the current object ($this) since the methods are acting on it in succession. Quote Link to comment https://forums.phpfreaks.com/topic/234790-multi-level-methods/#findComment-1206628 Share on other sites More sharing options...
The Little Guy Posted April 26, 2011 Author Share Posted April 26, 2011 ok, I looked at an example and it makes sense now. Let me try it! Quote Link to comment https://forums.phpfreaks.com/topic/234790-multi-level-methods/#findComment-1206629 Share on other sites More sharing options...
AbraCadaver Posted April 26, 2011 Share Posted April 26, 2011 Sorry, you must have read it before my edit. The method must return an object containing the next method. Normally $this. Quote Link to comment https://forums.phpfreaks.com/topic/234790-multi-level-methods/#findComment-1206630 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.