致命错误:..的声明必须与.. PHP兼容 [英] Fatal error: Declaration of .. must be compatible with .. PHP

查看:86
本文介绍了致命错误:..的声明必须与.. PHP兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下错误:

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()指出该方法不带任何参数,而实现Shoppingcart::addToCart(Product $product)要求必须将类型为Product的参数传递给方法.这意味着两个声明都不兼容,并且必须满足已实现的接口,PHP会抛出显示的错误.

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. This means that both declarations are incompatible and while the implemented interface must be satisfied PHP throws the shown error.

解决方案是将Ishoppingcart::addToCart()更改为Ishoppingcart::addToCart(Product $product),以使其需要类型为Product的参数,或者更改Shoppingcart::addToCart(Product $product)以不允许任何参数传递到方法中:Shoppingcart::addToCart(Product $product = null);

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);

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

The correct way depends on your application requirements.

这篇关于致命错误:..的声明必须与.. PHP兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆