Webbserverprogrammering 1

Show sourcecode

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

Webserver1/Ovningar/Klasser/

klass1.php
klass2och3.php
klass4.php
klass5och6.php

klass5och6.php

99 lines ASCII Windows (CRLF)
<!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 = (3.14 $this->get_radius() ** 3) / 3;
      }

      function 
set_radius($radius) {
        
parent::set_radius($radius);
        
$this->volume = (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.050.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>