Created
April 19, 2023 08:21
-
-
Save SmetDenis/a3307da0ec5a224e0e94dc5b283b88dc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Figures; | |
class Figure | |
{ | |
protected $color; | |
public function __construct($color) | |
{ | |
$this->color = $color; | |
} | |
public function saveToDb($connection) | |
{ | |
} | |
} | |
class Rectangle extends Figure | |
{ | |
public $height; | |
public $width; | |
public function __construct($height, $width, $color) | |
{ | |
$this->height = $height; | |
$this->width = $width; | |
parent::__construct($color); | |
} | |
public function saveToDb($connection) | |
{ | |
$sql = "INSERT INTO mytable (height, width, color) ". | |
"VALUES (".$this->height.", ".$this->width.", '".$this->color."')"; | |
if (mysqli_query($connection, $sql) !== true) | |
{ | |
throw new \Exception(); | |
} | |
return mysqli_insert_id($connection); | |
} | |
} | |
class Circle extends Figure | |
{ | |
public $r; | |
public function __construct($r, $color) | |
{ | |
$this->r = $r; | |
parent::__construct($color); | |
} | |
public function saveToDb($connection) | |
{ | |
$sql = "INSERT INTO mytable (radius, color) ". | |
"VALUES (".$this->r.", '".$this->color."')"; | |
if (mysqli_query($connection, $sql) !== true) | |
{ | |
throw new \Exception(); | |
} | |
return mysqli_insert_id($connection); | |
} | |
} | |
class Geometry | |
{ | |
public static function calculate(Figure $f) | |
{ | |
if ($f instanceof Rectangle) | |
return $f->height * $f->width; | |
elseif ($f instanceof Circle) | |
return $f->r * $f->r * 3.14; | |
throw new \Exception(); | |
} | |
public static function save($f, $connection) | |
{ | |
return $f->saveToDb($connection); | |
} | |
} | |
$con = mysqli_connect("localhost", "my_user", "my_password", "my_db"); | |
$f = new Rectangle(2, 3, 'red'); | |
echo "Square = ".Geometry::calculate($f).";"; | |
Geometry::save($f, $con); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment