Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/klasserochobjekt/
index.php
ovn_ko1 copy.php
ovn_ko1.php
ovn_ko2.php
ovn_ko3.php
ovn_ko4.php
ovn_ko5.php
ovn_ko7.php
ovn_ko7.php
74 lines UTF-8 Windows (CRLF)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
<?php
error_reporting(-1); // Report all type of errors
ini_set('display_errors', 1); // Display all errors
ini_set('output_buffering', 0); // Do not buffer outputs, write directly
?>
<!DOCTYPE html>
<html lang="sv">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>KLASSER</title>
</head>
<body>
<?php
class boll {
static $counter = 0;
private $ball_num;
private $color;
private $radius;
public function __construct($color, $radius){
$this->color = $color;
$this->radius = $radius;
self::$counter += 1;
$this->ball_num = self::$counter;
}
public function getInfo(){
// Används inte
return("Den här bollen är boll nr $this->ball_num, är $this->color och har radien $this->radius l.e.<br>");
}
public function getBallCount(){
return(self::$counter);
}
public function getBallNum(){
return($this->ball_num);
}
}
class bounceball extends boll {
private $bounciness;
function __construct($color, $radius, $bounciness)
{
parent::__construct($color,$radius);
$this->bounciness = $bounciness;
}
public function bounce(){
echo("Boing. Studsade $this->bounciness meter upp <br>");
}
}
class bowlingball extends boll {
public function bounce(){
echo("BANG. Inte en studsboll:(<br>");
}
}
$bollar = [];
$bollar[0] = new bounceball("lila", 2, 10);
$bollar[1] = new bowlingball("grön", 123);
$bollar[2] = new bounceball("blå", 123456787654323456, 2);
for ($i = 0; $i<count($bollar); $i++){
echo("Boll " . $bollar[$i]->getBallNum() . "/" . $bollar[$i]->getBallCount() . "<br>");
echo($bollar[$i]->getInfo());
$bollar[$i]->bounce();
echo("<br>");
}
?>
</body>
</html>