27

我收到以下错误:

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

5 回答 5

35

Ishoppingcart::addToCart()声明该方法不带任何参数,而实现Shoppingcart::addToCart(Product $product)要求Product必须将类型参数传递给该方法。这意味着两个声明是不兼容的,而实现的接口必须满足 PHP 抛出所示的错误。

解决方案是要么更改Ishoppingcart::addToCart()Ishoppingcart::addToCart(Product $product)它需要一个类型的参数,Product要么更改Shoppingcart::addToCart(Product $product)为不允许将参数传递给方法Shoppingcart::addToCart(Product $product = null)

正确的方法取决于您的应用程序要求。

于 2012-06-30T10:23:28.937 回答
6

子类中公共函数的声明必须与其父类的声明相匹配:

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

您不能向签名添加参数。

这与里氏替换原理密切相关。

于 2012-06-30T10:21:19.850 回答
2

接口 Ishoppingcart 似乎定义了不带参数的 addToShoppingCart,但类 Shoppingcart 定义了相同的函数,以 Product 作为参数。我想接口中的方法也应该将 Product 作为参数。

于 2012-06-30T10:21:37.020 回答
2

我通过升级到最新的 php 版本解决了这个问题

于 2020-05-27T07:00:06.643 回答
0

不适用于此特定问题,但如果有人遇到此问题,请确保包含接口指定的返回值。

具有如下功能的接口:

abstract public function crawled(Something $some): void;

需要实现为:

public function crawled(Something $some): void {
}

就我而言,我错过了: void返回类型。

于 2021-12-06T21:54:50.753 回答