致命错误:.. 声明必须与.. PHP 兼容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11273313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:10:15  来源:igfitidea点击:

Fatal error: Declaration of .. must be compatible with .. PHP

phpfatal-error

提问by MOTIVECODEX

I'm getting the following error:

我收到以下错误:

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

What could be the problem? I can't find it 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
?>

回答by Stefan Gehrig

Ishoppingcart::addToCart()states that the method does not take any parameter, while the implementation Shoppingcart::addToCart(Product $product)requires that a parameter of type Productmust be passed into the method. This means that both declarations are incompatible and while the implemented interface must be satisfied PHP throws the shown error.

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

Solution would be to either change Ishoppingcart::addToCart()to Ishoppingcart::addToCart(Product $product)so that it requires a parameter of type Productor to change Shoppingcart::addToCart(Product $product)to allow no parameter to passed into the method: Shoppingcart::addToCart(Product $product = null);

解决方案将是要么改变Ishoppingcart::addToCart()Ishoppingcart::addToCart(Product $product)使得它需要类型的参数Product,或改变Shoppingcart::addToCart(Product $product),以允许没有参数传递到方法:Shoppingcart::addToCart(Product $product = null);

The correct way depends on your application requirements.

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

回答by Corbin

The declaration of a public function in a sub class must match that of its parent:

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

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

You cannot add a parameter to the signature.

您不能向签名添加参数。

This is closely related to the Liskov substitution principle.

这与Liskov 替换原则密切相关。

回答by Jari

Interface Ishoppingcart seems to define addToShoppingCart without parameters, but class Shoppingcart defines the same function taking Product as a parameter. I suppose the method in the interface should take Product as a parameter as well.

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

回答by Ansyori

I resolved this issue by upgrade to latest php version

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