2012-06-30 84 views
13

我收到以下錯誤兼容:致命錯誤:..申報必須與.. PHP

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

可能是什麼問題呢?我找不到它 腳本:

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

回答

20

Ishoppingcart::addToCart()指出該方法不採用任何參數,而實施Shoppingcart::addToCart(Product $product)要求必須將類型Product的參數傳遞給該方法。這意味着這兩個聲明都是不兼容的,而且必須滿足實現的接口PHP會拋出顯示的錯誤。

溶液。將要麼改變Ishoppingcart::addToCart()Ishoppingcart::addToCart(Product $product)以便它需要Product類型的參數或改變Shoppingcart::addToCart(Product $product)以允許沒有參數傳遞到方法:Shoppingcart::addToCart(Product $product = null);

正確的方法取決於您的應用需求。

+0

兩種方式都行,謝謝我會用'= null' – MOTIVECODEX

+10

@ F4LLCON在我看來,使用默認值NULL是一種破解這打破了界面的目的。我期望一個接口有一個「這就是*這個方法的簽名」的合約。「我覺得要麼接口應該有參數,要麼兩個方法的命名應該不同。 – Corbin

+0

我完全按照我書中的說明操作。但它沒有奏效。我現在將使用黑客技術,因爲更改某些內容會影響腳本中的其他內容。 – MOTIVECODEX

4

在子類中的公共功能必須與其父的聲明:

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

您不能將參數添加到簽名。

這與Liskov substitution principle密切相關。

+0

謝謝Stefan Gehrig的解決方案出來了 – MOTIVECODEX

2

接口Ishoppingcart似乎沒有參數定義addToShoppingCart,但類Shoppingcart定義了以Product作爲參數的相同函數。我想接口中的方法也應該將Product作爲參數。

+0

謝謝Stefan Gehrig的解決方案制定出 – MOTIVECODEX