2012-06-30 21 views
13

estoy recibiendo el siguiente error:Fatal error: Declaración de .. debe ser compatible con PHP ..

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

¿Cuál podría ser el problema? No puedo encontrarlo guión:

<?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 
?> 

Respuesta

20

Ishoppingcart::addToCart() indica que el método no toma ningún parámetro, mientras que la implementación Shoppingcart::addToCart(Product $product) requiere que se pase al parámetro un parámetro de tipo Product. Esto significa que ambas declaraciones son incompatibles y mientras que la interfaz implementada debe cumplirse, PHP arroja el error mostrado.

solución sería o bien cambiar Ishoppingcart::addToCart()-Ishoppingcart::addToCart(Product $product) de modo que se requiere un parámetro de tipo Product o para cambiar Shoppingcart::addToCart(Product $product) para permitir ningún parámetro a pasado en el método: Shoppingcart::addToCart(Product $product = null);

La manera correcta depende de los requisitos de su aplicación.

+0

Ambas formas funcionan, gracias las voy a utilizar '= null' – MOTIVECODEX

+10

@ F4LLCON En mi opinión, utilizando el valor predeterminado es NULL es un hack eso derrota el propósito de una interfaz. Espero que una interfaz tenga un contrato de "esto es * exactamente * la firma de este método". Creo que la interfaz debe tener el parámetro o los dos métodos deben tener un nombre diferente. – Corbin

+0

Estoy siguiendo exactamente las instrucciones en mi libro. Pero no funcionó. Usaré el truco por ahora porque cambiar algo afectará otras cosas en el guión. – MOTIVECODEX

4

La declaración de una función pública en una subclase debe coincidir con el de su padre:

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

No se puede agregar un parámetro a la firma.

Esto está estrechamente relacionado con el Liskov substitution principle.

+0

Gracias, soluciones de Stefan Gehrig elaborados – MOTIVECODEX

2

Interfaz Ishoppingcart parece definir addToShoppingCart sin parámetros, pero la clase Shoppingcart define la misma función tomando Product como parámetro. Supongo que el método en la interfaz también debería tomar el Producto como parámetro.

+0

Gracias, soluciones de Stefan Gehrig elaborados – MOTIVECODEX

Cuestiones relacionadas