简体   繁体   English

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

[英]Fatal error: Declaration of .. must be compatible with .. PHP

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

Ishoppingcart::addToCart() states that the method does not take any parameter, while the implementation Shoppingcart::addToCart(Product $product) requires that a parameter of type Product must be passed into the method. Ishoppingcart::addToCart()声明该方法不接受任何参数,而实现Shoppingcart::addToCart(Product $product)要求必须将Product类型的参数传递到该方法中。 This means that both declarations are incompatible and while the implemented interface must be satisfied PHP throws the shown error.这意味着这两个声明是不兼容的,而实现的接口必须满足 PHP 会抛出显示的错误。

Solution would be to either change Ishoppingcart::addToCart() to Ishoppingcart::addToCart(Product $product) so that it requires a parameter of type Product or 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.正确的方法取决于您的应用要求。

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 替换原则密切相关。

Interface Ishoppingcart seems to define addToShoppingCart without parameters, but class Shoppingcart defines the same function taking Product as a parameter.接口 Ishoppingcart 似乎定义了不带参数的 addToShoppingCart,但类 Shoppingcart 定义了相同的函数,以 Product 为参数。 I suppose the method in the interface should take Product as a parameter as well.我想接口中的方法也应该将 Product 作为参数。

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

Doesn't apply to this particular question, but if someone there is having this issue, make sure to include the return value if the Interface specifies it.不适用于此特定问题,但如果有人遇到此问题,请确保在接口指定时包含返回值。

An interface with a function like this:具有如下功能的接口:

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

needs to be implemented as:需要实现为:

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

In my case I was missing the : void return type.就我而言,我缺少: void返回类型。

This can also happen if Intelephense sees multiple classes with the same name but with different signatures.如果 Intelephense 看到多个具有相同名称但具有不同签名的类,也会发生这种情况。 They may not be in PHP's scope, but Intelephense allows specifying additional directories and seems to pick up backup directories and the like as well.它们可能不在 PHP 的 scope 中,但 Intelephense 允许指定其他目录,并且似乎也可以选择备份目录等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PHP 致命错误:...的声明必须与 - PHP Fatal error: Declaration of ... must be compatible with 致命错误:..的声明必须与.. PHP兼容(所需参数:规范的扩展类) - Fatal error: Declaration of .. must be compatible with .. PHP (parameter required: extended class of specification) Composer致命错误:Fxp声明...必须与第334行的AbstractAssetsRepository.php兼容 - Composer Fatal error: Declaration of Fxp… must be compatible with …AbstractAssetsRepository.php on line 334 致命错误:TableDisplayOptions :: load()的声明必须与JTableInterface :: load()的声明兼容 - Fatal error: Declaration of TableDisplayOptions::load() must be compatible with that of JTableInterface::load() 致命错误:AdminUserRole :: getAccessibleEmployeeIds()的声明必须与AbstractUserRole :: getAccessibleEmployeeIds()的声明兼容 - Fatal error: Declaration of AdminUserRole::getAccessibleEmployeeIds() must be compatible with that of AbstractUserRole::getAccessibleEmployeeIds() 致命错误:registerContainerConfiguration的声明必须与Kernel :: registerContainerConfiguration的声明兼容 - Fatal error: Declaration of registerContainerConfiguration must be compatible with that of Kernel::registerContainerConfiguration 致命错误:EasyBlogTableMediaManager :: bind()的声明必须与JTableInterface :: bind()的声明兼容 - Fatal error: Declaration of EasyBlogTableMediaManager::bind() must be compatible with that of JTableInterface::bind() PHP FatalErrorException-“”的声明必须与“”兼容 - PHP FatalErrorException - Declaration of ' ' must be compatible with ' ' 致命错误:Foo::__toString() 的声明:void must be compatible with Stringable::__toString(): string - Fatal error: Declaration of Foo::__toString(): void must be compatible with Stringable::__toString(): string PHP 声明 class 必须与抽象 class 兼容 - PHP declaration of class must be compatible with abstract class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM