DEV Community

Programming Dive
Programming Dive

Posted on

3

Method Chaining in PHP

In this tutorial, we’ll take a look at Method Chaining in PHP. Method Chaining is nothing but the concatenation of multiple methods to increase readability of code and avoid putting all the code in the single function.

One of the major changes between PHP4 and PHP5 is that in PHP5 method can return objects. We will first see the basics of method chaining and then we’ll look into examples with explanation.

Method Chaining is mainly used to implement Fluent Interface in an object-oriented API to achieve code readability. The term Fluent Interface was first invented by Eric Evans and Martin Fowler back in 2005. Note that method chaining is not Fluent Interface. Rather Fluent Interface uses method chaining.

Using Fluent Interface, now we can allow multiple methods to be called on the same object using $this variable.

https://programmingdive.com/method-chaining-in-php/

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay