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

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

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

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

When you encounter a Fatal error in PHP stating that a method declaration is incompatible with an interface or parent class, it is almost always a signature mismatch. The runtime checks that every concrete implementation strictly adheres to the contract defined by its parent or interface. If you change a parameter type, add a new argument, or alter the return type without updating the parent or interface, PHP throws this error immediately upon encountering the definition of the child class.

In the specific case provided, the error message points to line 118 in klassen.php: Fatal error: Declaration of Shoppingcart::addToCart() must be compatible with that of Ishoppingcart::addToCart().

Let's dissect the code causing this failure. The interface Ishoppingcart defines the method signature as follows:

interface Ishoppingcart {
    public function addToCart(); 
    // ... other methods
}

Notice that the addToCart() method in the interface accepts no parameters and returns nothing. However, when you look at the Shoppingcart class implementing this interface, the implementation looks like this:

abstract class Shoppingcart implements Ishoppingcart {
    protected $producten = array();

    public function addToCart( Product $product) {
        $this->producten[] = $product;
    }
}

Here lies the root cause. The implementation declares a parameter (Product $product) while the interface expects zero parameters. PHP is strict about this contract. Even if you never intend to call addToCart() with arguments in your current code flow, the compiler (PHP's Zend engine) cares only about the signature match. You cannot silently ignore required interface parameters or change their types.

Why PHP Enforces This Strictly

This behavior exists because interfaces define a public API contract. If you are using dependency injection or testing frameworks that instantiate Shoppingcart and call $cart->addToCart(), they will likely pass an argument. If the interface expects no arguments but the implementation accepts one, the code might run in some contexts but fail others, or worse, hide bugs where the caller assumes a different signature than what was promised.

To resolve this specific error, you have two main paths depending on your design intent:

  1. Update the Interface: If adding a product is necessary for the cart to function, the interface itself must be updated to reflect that requirement.
  2. Update the Implementation (if allowed): If addToCart was intended to accept parameters but the interface was written incorrectly or simplistically, you must align them. You cannot just fix the child; you must fix the parent contract.

Here is how the corrected code should look if adding a product is a required feature:

interface Ishoppingcart {
    // The interface must now declare the parameter type
    public function addToCart(Product $product); 
}

abstract class Shoppingcart implements Ishoppingcart {
    protected $producten = array();

    public function addToCart(Product $product) {
        $this->producten[] = $product;
    }
}

Additional Issues in the Provided Code

While fixing the compatibility error is the primary task, there are other subtle syntax errors in the provided Product class that would prevent the script from running even after fixing the interface issue.

In the constructor of the Product class:

function __construct($id, $merk, ...) {
    // ...
    $this->beschrijving = $beschrijving; 
}

The variable $beschrijving is used directly in the assignment, but it was not passed as an argument to the constructor. The definition lists $beschrijving as a parameter (implicitly via the list), but looking closely at the __construct signature:

function __construct($id, $merk, $model, $prijs, $aantal, $korting)

The $beschrijving argument is missing from the parameter list entirely. Additionally, in the getId() getter method, there is a logic error:

public function getId(){
    return $this->id = $id; // Incorrect assignment and variable usage
}

This line attempts to assign $id (which is not defined in this scope) to $this->id and then return the result of that assignment. It should simply be return $this->id;. These are common pitfalls when refactoring or writing getters/setters quickly.

Best Practices for Interface Compatibility

When working with interfaces, always remember that the implementation is bound by the interface's signature. If you are using a framework like Laravel, this concept extends to trait implementations and service contracts defined in the Contracts namespace.

If you find yourself needing to change a method signature frequently, consider whether an abstract class would be more appropriate than an interface for that specific scenario, as abstract classes allow for partial implementation while still enforcing the contract. However, once an interface is established, any deviation in the implementing class will result in this exact fatal error until the signatures are reconciled.

Always verify your implements clause against the actual content of the interface file. A small mismatch in parameter names or types can stop a large application cold.

Our network