LearnChaining methods and properties

Chaining methods and properties

In the previous tutorial, we learned to use the $this keyword to approach properties and methods within the scope of the class. In this chapter we will learn that, when a class's methods return the $this keyword, they can be chained together to create much more streaming code.

For instance, 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 is dependent upon the number of miles we have driven in our car, as well as the amount of fuel that we put in the tank.

In order to achieve our goal, we are going to put a public property $tank to our class that represents the number of gallons of fuel that we have in the car's tank.

class Car {
  public $tank;
}

We must also add two methods to our Car class:

1. The fill() method adds gallons of fuel to our car's tank.

2. The ride() method calculates how much fuel we consume when we ride a certain distance, and then subtracts it from the tank. In our example, we assume that the car consumes 1 gallon of fuel every 50 miles.

class Car {
 
  public $tank;
 
  // Add gallons of fuel to the tank when we fill it.
  public function fill($float)
  {
    $this-> tank += $float;
  }
  
  // Substract gallons of fuel from the tank as we ride the car.
  public function ride($float)
  {
    $miles = $float;
    $gallons = $miles/50;
    $this-> tank -= $gallons;
  }
}

As we would like our code to look elegant, we will chain the methods and properties. Note the arrows in the code.

$tank = $car -> fill(10) -> ride(40) -> tank;

In words: How much fuel do we have left in our tank after putting in 10 gallons, and driving 40 miles?

In order for us to be able to perform the chaining, the methods should return the object and, since we are inside the class, the methods should return the $this keyword.

In the code below, we can see how each method returns the $this keyword in order to allow the chaining.

class Car {
 
  public $tank;
  
  // Add gallons of fuel to the tank when we fill it.
  public function fill($float)
  {
    $this-> tank += $float;
 
    return $this;
  }
  
  // Substract gallons of fuel from the tank as we ride the car.
  public function ride($float)
  {
    $miles = $float;
    $gallons = $miles/50;
    $this-> tank -= ($gallons);
 
    return $this;
  }
}

Now, we can create an object from the Car class with the name of $bmw and find out the number of gallons of fuel left in our car's tank after we have filled the tank with 10 gallons of fuel and driven 40 miles.

// Create a new object from the Car class.
$bmw = new Car();
 
// Add 10 gallons of fuel, then ride 40 miles, 
// and get the number of gallons in the tank. 
$tank = $bmw -> fill(10) -> ride(40) -> tank;
 
// Print the results to the screen.
echo "The number of gallons left in the tank: " . $tank . " gal.";

Result:
The number of gallons left in the tank: 9.2 gal.

Conclusion

In this tutorial, you learned that you can chain properties and methods to provide for a fluent code and improve its readability. Click here to practice the subject. In the next tutorial, your will learn how to restrict access to the properties and methods inside the classes by using the private and public keywords.

comments powered by Disqus