NotionCommotion Posted June 7, 2016 Share Posted June 7, 2016 Is chaining methods considered good or bad practice? Please explain why or why not. If it is considered okay, please provide recommended formatting. Thanks return $db->prepare("SELECT a, b, c FROM mytable WHERE id=:id") ->execute(["id"=>123]). ->fetchAll(PDO:FETCH_OBJ); Quote Link to comment https://forums.phpfreaks.com/topic/301312-is-chaining-methods-considered-good-or-bad-practice/ Share on other sites More sharing options...
Solution Jacques1 Posted June 7, 2016 Solution Share Posted June 7, 2016 It's considered good practice, simply because it's both nice to read and nice to write. There's even a design pattern for it: The Fluent Interface. The only reason why PDO isn't fluent is that it supports legacy error handling and uses the return values to indicate errors. Without this baggage, I'm sure our code would look exactly as you suggested (including the formatting). Quote Link to comment https://forums.phpfreaks.com/topic/301312-is-chaining-methods-considered-good-or-bad-practice/#findComment-1533473 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.