Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
dvprog_14 [2019-09-09 09:26]
Daniel Viström
dvprog_14 [2022-07-18 13:20] (current)
Line 1: Line 1:
-====== Genomgång 11 - Objektorienterad programmering ======+====== Genomgång - Objektorienterad programmering ======
  
 Viktgt att du lär dig vad som menas med följande och att du kan skilja dem åt: Viktgt att du lär dig vad som menas med följande och att du kan skilja dem åt:
Line 12: Line 12:
  
 ==== Exempel 1 ==== ==== Exempel 1 ====
- 
 <code php> <code php>
 <?php <?php
- +include 'counter.php';
-// Klassen kan ligga i samma fil som huvudprogrammet, men försök att lägga det i separat fil som i exempel 2. +
- +
-class Counter{ +
- +
-  private $value; +
-   +
-  public function __construct(){ +
-    $this->value = 0; +
-  } +
-   +
-  public function klick(){ +
-    $this->value++; +
-  } +
-   +
-  public function getValue(){ +
-    return $this->value; +
-  } +
-   +
-  public function zero(){ +
-    $this->value = 0; +
-  } +
-+
- +
- +
-// Här börjar huvudprogrammet. +
 include 'head.php'; include 'head.php';
  
Line 65: Line 38:
 include 'foot.php'; include 'foot.php';
  
 +</code>
 +
 +Klassen **Counter** ligger i en separat fil, **counter.php**. \\ 
 +Klassen kan ligga i samma fil som huvudprogrammet, men försök att lägga det i separat fil.
 +
 +<code php>
 +<?php
 +class Counter{
 +
 +  private $value;
 +  
 +  public function __construct(){
 +    $this->value = 0;
 +  }
 +  
 +  public function klick(){
 +    $this->value++;
 +  }
 +  
 +  public function getValue(){
 +    return $this->value;
 +  }
 +  
 +  public function zero(){
 +    $this->value = 0;
 +  }
 +}
 </code> </code>
  
Line 71: Line 71:
 <code php> <code php>
 <?php <?php
-include 'head.php'; 
 include 'animals.php'; include 'animals.php';
 +include 'head.php';
  
 $a1 = new Animal();   // Skapar en ny instans (objekt) av klassen Animal. Vikten blir 0 (se klassen). $a1 = new Animal();   // Skapar en ny instans (objekt) av klassen Animal. Vikten blir 0 (se klassen).