Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

ramverket/exercises/klasser/

ovn_kls1.php
ovn_kls2.php
ovn_kls3.php
ovn_kls4.php
ovn_kls5.php
ovn_kls6.php
ovn_kls7.php

ovn_kls6.php

80 lines UTF-8 Windows (CRLF)
<?php
  
// Title: Klasser 6
  
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 6</title>
</head>
<body>
  <?php 
    
class Ball {
      protected 
$color;
      protected 
$radius;

      public function 
__construct($color$radius) {
        
$this->color $color;
        
$this->radius $radius;
      }

      public function 
getColor() {
        return 
$this->color;
      }

      public function 
getRadius() {
        return 
$this->radius;
      }
    }

    class 
BasketBall extends Ball {
      private 
$type;
      public function 
__construct($color$radius) {
        
parent::__construct($color$radius);
        
$this->type "Basketboll";
      }

      public function 
getType() {
        return 
$this->type;
      }

      public function 
getColor() {
        return 
$this->color;
      }
    }

    class 
Football extends Ball {
      private 
$type;
      public function 
__construct($color$radius) {
        
parent::__construct($color$radius);
        
$this->type "Fotboll";
      }

      public function 
getType() {
        return 
$this->type;
      }

      public function 
getColor() {
        return 
$this->color;
      }
    }

    
$items = [];

    
array_push($items, new BasketBall("Röd"10));
    
array_push($items, new Football("Gul"20));

    for (
$i 0$i count($items); $i++) {
      
$item $items[$i];
      echo 
$item->getType() . "<br>";
      echo 
"Färg: " $item->getColor() . "<br>";
      echo 
"Radius: " $item->getRadius() . "<br><hr>";
    }
  
?>
</body>
</html>