Show sourcecode
The following files exists in this folder. Click to view.
klasser.php
klasser1.php
klasser2.php
klasser3.php
klasser4.php
klasser5.php
klasser6.php
klasser7.php
klasser8.php
klasser8.php
97 lines UTF-8 Windows (CRLF)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Klasser</title>
<style>
div {
border-radius:100%;
display: flex;
}
</style>
</head>
<body>
<?php
class Boll {
public static $count = 0;
public $färg;
public $radie;
private $num;
public function __construct($radie, $färg) {
Boll::$count++;
$this->färg = $färg;
$this->radie = $radie;
$this->num = $this::$count;
}
protected function info() {
echo "<br>Ball number $this->num is $this->färg and has a radius of $this->radie"."m.";
}
public static function get_amount() {
return Boll::$count;
}
public function draw() {
echo "<div style='border-radius:100%; background-color:$this->färg;width:".$this->radie."vw;height:".$this->radie."vw;'></div>";
}
}
class Fotboll extends Boll {
private $färg2;
public function __construct($radie, $färg, $färg2) {
parent::__construct($radie, $färg);
$this->färg2 = $färg2;
}
public function info() {
parent::info();
echo " It has a $this->färg2 checker pattern.";
}
public function draw() {
echo "<div style='background-color:$this->färg;width:".$this->radie."vw;height:".$this->radie."vw;'>";
$side = $this->radie / 4;
for ($i = 0; $i < 4; $i++) {
echo "<span style='background-color:$this->färg2;width:".$side."vw;height:".$side."vw;></span>";
}
echo "</div>";
}
}
class Studsboll extends Boll {
public function __construct($radie, $färg) {
parent::__construct($radie, $färg);
}
public function info() {
parent::info();
echo " It is bouncy.";
}
}
$bollar = [
new Fotboll(5, "red", "black"),
new Fotboll(2, "green", "white"),
new Studsboll(7, "yellow"),
new Studsboll(15, "purple")
];
foreach ($bollar as $boll) {
$boll->info();
$boll->draw();
}
echo "<hr>Det finns ".Boll::get_amount()." bollar.";
?>
</body>
</html>