Skip to content

Instantly share code, notes, and snippets.

@kangmasjuqi
Created May 2, 2021 04:33
Show Gist options
  • Save kangmasjuqi/f0ebbde3093c71dc4e5da9396aa3fa72 to your computer and use it in GitHub Desktop.
Save kangmasjuqi/f0ebbde3093c71dc4e5da9396aa3fa72 to your computer and use it in GitHub Desktop.
PHP OOP with Polymorphism
<?php
// POLYMORPHISM
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
interface Shape{
public function calcArea();
}
class Circle implements Shape{
private $r = 0;
public function __construct($r){
$this->r = $r;
}
public function calcArea(){
return 3.14 * $this->r * $this->r;
}
}
class Rectangle implements Shape{
private $x = 0;
private $y = 0;
public function __construct($x, $y){
$this->x = $x;
$this->y = $y;
}
public function calcArea(){
return $this->x * $this->y;
}
}
$c = new Circle(10);
$r = new Rectangle(7, 10);
$cArea = $c->calcArea();
$rArea = $r->calcArea();
echo '<pre>';
var_dump($cArea);
var_dump($rArea);
echo '</pre>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment