2012-06-30 16 views
13

sto ottenendo il seguente errore:Fatal error: Dichiarazione di .. deve essere compatibile con PHP ..

Fatal error: Declaration of Shoppingcart::addToCart() must be compatible with that of Ishoppingcart::addToCart() in klassen.php on line 118 

Quale potrebbe essere il problema? Io non riesco a trovarlo script:

<?php 
// begin 
interface Ishoppingcart{ 
    public function addToCart(); 
    public function printOverzicht(); 
} 
abstract class Shoppingcart implements Ishoppingcart 
{ 
    protected $producten = array(); 

    public function addToCart(Product $product) { 
     $this->producten[] = $product; 
    } 
} 
class Myshoppingcart extends Shoppingcart { 
    public function printOverzicht(){ 
     echo ("<table border=1> 
     <tr> 
     <td colspan='7'>Bestellingoverzicht</td> 
     </tr> 
     <tr> 
     <td bgcolor=#cccccc> Product ID</td> 
     <td bgcolor=#cccccc> Beschrijving</td> 
     <td bgcolor=#cccccc> Merk</td> 
     <td bgcolor=#cccccc> Model</td> 
     <td bgcolor=#cccccc> Prijs</td> 
     <td bgcolor=#cccccc> Aantal</td> 
     <td bgcolor=#cccccc> Korting</td> 
     </tr>"); 

     foreach($this->producten as $product){ 
      $rij = ""; 
      $rij .= "<tr>"; 
      $rij .= "<td>".$product->getID()."</td>"; 
      $rij .= "<td>".$product->getBeschrijving()."</td>"; 
      $rij .= "<td>".$product->getMerk()."</td>"; 
      $rij .= "<td>".$product->getModel()."</td>"; 
      $rij .= "<td>".$product->getPrijs()."</td>"; 
      $rij .= "<td>".$product->getAantal()."</td>"; 
      $rij .= "<td>".$product->getKorting()."</td>"; 
      $rij .= "</tr>"; 
      echo ($rij); 
     } 
     echo ("</table>"); 
    } 
} 
class Product { 
    private $id; 
    private $beschrijving; 
    private $merk; 
    private $model; 
    private $prijs; 
    private $aantal; 
    private $korting; 

    function __construct($id, 
         $merk, 
         $model, 
         $prijs, 
         $aantal, 
         $korting){ 
     $this->id = $id; 
     $this->beschrijving = $beschrijving; 
     $this->merk = $merk; 
     $this->model = $model; 
     $this->prijs = $prijs; 
     $this->aantal = $aantal; 
     $this->korting = $korting; 
     echo ("<br />Nieuw Product object $beschrijving wordt aangemaakt"); 
         } 
    public function __destruct(){ 
     // voer benodigde acties uit 
     echo ("<br />Product object $this->beschrijving wordt verwijderd"); 
    } 
    // set function 
    public function setId($id){ 
     $this->id = $id; 
    } 
    public function setBeschrijving($beschrijving){ 
     $this->beschrijving = $beschrijving; 
    } 
    public function setMerk($merk){ 
     $this->merk = $merk; 
    } 
    public function setModel($model){ 
     $this->model = $model; 
    } 
    public function setPrijs($prijs){ 
     $this->prijs = $prijs; 
    } 
    public function setAantal($aantal){ 
     $this->aantal = $aantal; 
    } 
    public function setKorting($korting){ 
     $this->korting = $korting; 
    } 
    // get function 
    public function getId(){ 
     return $this->id = $id; 
    } 
    public function getBeschrijving(){ 
     return $this->beschrijving; 
    } 
    public function getMerk(){ 
     return $this->merk; 
    } 
    public function getModel(){ 
     return $this->model; 
    } 
    public function getPrijs(){ 
     return $this->prijs; 
    } 
    public function getAantal(){ 
     return $this->aantal; 
    } 
    public function getKorting(){ 
     return $this->korting; 
    } 

    // printProductInfo 
    public function printProductInfo(){ 
    $rij = "<tr><td>$this->id</td>"; 
    $rij .= "<td>$this->beschrijving</td>"; 
    $rij .= "<td>$this->merk</td>"; 
    $rij .= "<td>$this->model</td>"; 
    $rij .= "<td>$this->prijs</td>"; 
    $rij .= "<td>$this->aantal</td>"; 
    $rij .= "<td>$this->korting</td>"; 
    echo ($rij); 
    } 
} 
// einde 
?> 

risposta

20

Ishoppingcart::addToCart() afferma che il metodo non accetta alcun parametro, mentre l'implementazione Shoppingcart::addToCart(Product $product) richiede che un parametro di tipo Product debba essere passato nel metodo. Ciò significa che entrambe le dichiarazioni sono incompatibili e mentre l'interfaccia implementata deve essere soddisfatta, PHP genera l'errore mostrato.

soluzione sarebbe quella di cambiare sia Ishoppingcart::addToCart() a Ishoppingcart::addToCart(Product $product) modo che richiede un parametro di tipo Product o per cambiare Shoppingcart::addToCart(Product $product) di non consentire parametro passato nel metodo: Shoppingcart::addToCart(Product $product = null);

Il modo corretto dipende dai requisiti dell'applicazione.

+0

Entrambi i modi di lavoro, grazie userò '= null' – MOTIVECODEX

+10

@ F4LLCON A mio parere, utilizzando il valore predefinito è NULL è un hack che sconfigge lo scopo di un'interfaccia. Mi aspetto che un'interfaccia abbia un contratto di "questo è * esattamente * la firma di questo metodo". Ritengo che l'interfaccia debba avere il parametro, altrimenti i due metodi dovrebbero essere denominati in modo diverso. – Corbin

+0

Seguo esattamente le istruzioni nel mio libro. Ma non ha funzionato. Per ora utilizzerò l'hack perché modificare qualcosa influirà su altre cose nella sceneggiatura. – MOTIVECODEX

4

La dichiarazione di una funzione pubblica in una classe sub deve corrispondere a quella del suo genitore:

public function addToCart(); 
public function addToCart(Product $product) 

non è possibile aggiungere un parametro per la firma.

Questo è strettamente correlato allo Liskov substitution principle.

+0

Grazie, le soluzioni di Stefan Gehrig elaborati – MOTIVECODEX

2

Interface Ishoppingcart sembra definire addToShoppingCart senza parametri, ma class Shoppingcart definisce la stessa funzione prendendo Product come parametro. Suppongo che il metodo nell'interfaccia dovrebbe prendere anche il prodotto come parametro.

+0

Grazie, le soluzioni di Stefan Gehrig elaborati – MOTIVECODEX

Problemi correlati