Chaining Methods and Properties

Learn how to chain methods and properties.

We'll cover the following

We can chain properties and methods to provide fluent code and improve its readability. When a class’s methods return the $this keyword, they can be chained together to create more streaming code.

How to chain methods and properties?

For example, in our Car class, let’s say that we want to measure how much fuel we have in our car’s tank. The amount of fuel in the tank depends on the number of miles we’ve driven in our car and the amount of fuel that we put in the tank.

In order to achieve our goal, we’ll add a public property $tank to our class. This represents the number of gallons of fuel that we have in the car’s tank.

Get hands-on with 1200+ tech skills courses.