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 [2020-05-18 20:17]
Daniel Viström
dvprog_14 [2020-05-18 20:20]
Daniel Viström
Line 12: Line 12:
  
 ==== Exempel 1 ==== ==== Exempel 1 ====
- 
-<code php> 
-<?php 
- 
-// Klassen kan ligga i samma fil som huvudprogrammet, men försök att lägga det i separat fil. 
- 
-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 php> <code php>
 <?php <?php
 +include 'counter.php';
 include 'head.php'; include 'head.php';
  
Line 66: 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 72: 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).