Show sourcecode
The following files exists in this folder. Click to view.
klass1.php
klass2och3.php
klass4.php
klass5och6.php
klass5och6.php
99 lines ASCII Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Even more classier</title>
</head>
<body>
<?php
class Circle {
private $color = "red";
private $radius = 15.0;
protected static $name = "Circle";
private static $instances = 0;
private static $pi = 3.14;
private $area;
function __construct($color, $radius) {
$this->color = $color;
$this->set_radius($radius);
static::$instances += 1;
}
function set_radius($radius) {
$this->radius = $radius;
$this->area = Circle::$pi * $radius;
}
function get_radius() {
return $this->radius;
}
function get_info() {
return "Type: " . static::$name . ", color: " . $this->color . ", radius: " . $this->radius;
}
function get_area() {
return $this->area;
}
public static function get_instances() {
return static::$instances;
}
}
class Sphere extends Circle {
private $volume;
protected static $name = "Sphere";
function __construct($color, $radius) {
parent::__construct($color, $radius);
$this->volume = (4 * 3.14 * $this->get_radius() ** 3) / 3;
}
function set_radius($radius) {
parent::set_radius($radius);
$this->volume = (4 * 3.14 * $this->get_radius() ** 3) / 3;
}
function get_volume() {
return $this->volume;
}
function get_info() {
return parent::get_info() . ", volume: " . round($this->volume);
}
}
Class Cylinder extends Circle {
protected static $name = "Cylinder";
private $volume;
private $height;
function __construct($color, $radius, $height) {
parent::__construct($color, $radius);
$this->height = $height;
$this->volume = $this->get_area() * $height;
}
function set_radius($radius) {
parent::set_radius($radius);
$this->volume = $this->get_area() * $this->height;
}
function get_volume() {
return $this->volume;
}
function get_info() {
return parent::get_info() . ", height:" . $this->height . ", volume: " . round($this->volume);
}
}
$circle = new Circle("Red", 20.0);
$sphere = new Sphere("Blue", 10.0);
$cylinder = new Cylinder("Yellow", 30.0, 50.0);
$things = [$circle, $sphere, $cylinder, new Sphere("Green", 15.0)];
foreach ($things as $value) {
echo "<p>" . $value->get_info() . "</p>";
}
echo "Amount of instances: " . Circle::get_instances();
?>
</body>
</html>