What is method chaining (fluent interface), and how is it implemented in PHP?
Last updated
Method chaining (a fluent interface) lets you call several methods on one object in a single expression, because each method ends with
return $this;so the next call continues on the same instance.It yields readable APIs like query builders, validators and HTTP clients.
Only configuring/mutating methods can chain. A method returning a real value (like a query result) ends the chain.
- https://www.geeksforgeeks.org/php/what-is-new-in-php-type-hinting-support-in-php-8/
- https://www.geeksforgeeks.org/php/php-interview-questions-and-answers/
- https://ariyathacademy.com/interview-questions.html
- https://www.freecodecamp.org/news/how-to-use-the-builder-pattern-in-python-a-practical-guide-for-devs/