Creating a PHP Class with Type Hinting and Return Types

How to create a PHP class with strict type, getters and setters and method return types.

 

class automobile {
	
  public int $speed;

function __construct() {
	
	$this->speed = 10;
	
}

function get_speed() : int {
	
	return $this->speed;
}

function set_speed(int $speed) {
	
	$this->speed = $speed;
}
	
}
  
$myCar = new automobile;

echo $myCar->get_speed();
$myCar->set_speed(20);
echo $myCar->get_speed();

  

Categorized in: