uncaught method fatal error php fatal-error

method - Error fatal: la declaración de.. debe ser compatible con.. PHP



uncaught error call to undefined function (3)

La declaración de una función pública en una subclase debe coincidir con la de su padre:

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

No se puede agregar un parámetro a la firma.

Esto está estrechamente relacionado con el principio de sustitución de Liskov .

Estoy recibiendo el siguiente error:

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

¿Cual podría ser el problema? No puedo encontrarlo 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 ?>


La interfaz Ishoppingcart parece definir addToShoppingCart sin parámetros, pero la clase Shoppingcart define la misma función que toma Producto como parámetro. Supongo que el método en la interfaz también debería tomar Producto como parámetro.


Ishoppingcart::addToCart() indica que el método no toma ningún parámetro, mientras que la implementación Shoppingcart::addToCart(Product $product) requiere que un parámetro de tipo Product se pase al método. Esto significa que ambas declaraciones son incompatibles y mientras que la interfaz implementada debe estar satisfecha, PHP arroja el error mostrado.

La solución sería cambiar Ishoppingcart::addToCart() por Ishoppingcart::addToCart(Product $product) para que requiera un parámetro de tipo Product o cambiar Shoppingcart::addToCart(Product $product) para no permitir que ningún parámetro pase a el método: Shoppingcart::addToCart(Product $product = null) ;

La forma correcta depende de los requisitos de su aplicación.