Skip to content

Instantly share code, notes, and snippets.

@kangmasjuqi
Created May 2, 2021 04:50
Show Gist options
  • Save kangmasjuqi/76a5032362a680281ed78680830d3db6 to your computer and use it in GitHub Desktop.
Save kangmasjuqi/76a5032362a680281ed78680830d3db6 to your computer and use it in GitHub Desktop.
PHP OOP with Abstract
<?php
// ABSTRACT
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
abstract class Vehicle{
protected function initialSetup(){
return $this->installChassis()
->installTire()
->installSteer();
}
protected function installChassis(){
var_dump("lets installChassis");
return $this;
}
protected function installTire(){
var_dump("lets installTire");
return $this;
}
protected function installSteer(){
var_dump("lets installSteer");
return $this;
}
abstract public function setupSeats();
}
class Car extends Vehicle{
public function __construct(){
var_dump("Car created");
$this->initialSetup();
$this->setupSeats();
}
public function setupSeats(){
var_dump("setup 7 seats for Car");
}
}
class Motorcycle extends Vehicle{
public function __construct(){
var_dump("Motorcycle created");
$this->initialSetup();
$this->setupSeats();
}
public function setupSeats(){
var_dump("setup 2 seats for Motorcycle");
}
}
echo "<pre>";
$c = new Car();
echo "</pre>";
echo "<pre>";
$m = new Motorcycle();
echo "</pre>";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment