Show sourcecode
The following files exists in this folder. Click to view.
klass1.php
klass2och3.php
klass4.php
klass5och6.php
klass4.php
84 lines ASCII Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Even classier</title>
</head>
<body>
<?php
class Circle {
public $color = "red";
protected $radius = 15.0;
public static $pi = 3.14;
protected $area;
function __construct($color, $radius) {
$this->color = $color;
$this->set_radius($radius);
}
function set_radius($radius) {
$this->radius = $radius;
$this->area = Circle::$pi * $radius;
}
function get_info() {
return "color: " . $this->color . ", radius:" . $this->radius;
}
}
class Sphere extends Circle {
protected $volume;
function __construct($color, $radius) {
parent::__construct($color, $radius);
$this->volume = (4 * 3.14 * $this->radius ** 3) / 3;
}
function set_radius($radius) {
parent::set_radius($radius);
$this->volume = (4 * 3.14 * $this->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 $volume;
protected $height;
function __construct($color, $radius, $height) {
parent::__construct($color, $radius);
$this->height = $height;
$this->volume = $this->area * $height;
}
function set_radius($radius) {
parent::set_radius($radius);
$this->volume = $this->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);
echo "<p>Circle: " . $circle->get_info() . "</p>";
echo "<p>Sphere: " . $sphere->get_info() . "</p>";
echo "<p>Cylinder: " . $cylinder->get_info() . "</p>";
?>
</body>
</html>