Method chaining
<?php
// method chaining
class Car {
public $tank;
public function fill ( $float ) {
$this->tank += $float;
return $this; // return the object
}
public function ride ( $float ) {
$miles = $float ;
$gallons = $miles / 50 ;
$this->tank -= $gallons;
return $this; // return the object
}
}
$bmw = new Car() ;
// after fill fills the tank with 10 and rides 40 miles, we have $bwm->tank
$tank = $bmw->fill(10)->ride(40)->tank;
echo "The number of gallons left in the tank " . $tank . ' gallons' ;
echo PHP_EOL;
class User {
public $firstName;
public $lastName;
public function hello() {
return "hello, " . $this->firstName;
}
public function register() {
echo $this->firstName . ' ' . $this->lastname . " has registered, " ;
return $this ;
}
public function mail() {
echo "emailed" ;
return $this;
}
}
$user = new User() ;
$user->firstName = 'Jane' ;
$user->lastname = 'Roe' ;
$user->register()->mail();