Can a class extend both a class and implement an Interface

Can a class extend both a class and implement an Interface

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

Stefan Bogdanescu

Founder & Senior Architect

The Syntax Order: Extending Classes and Implementing Interfaces

PHP enforces a strict syntax order when defining classes that inherit from multiple sources. You cannot mix the extends and implements keywords arbitrarily; their placement is determined by a specific rule that, if violated, results in a parse error.

The scenario you described involves a class attempting to extend the native mysqli class while simultaneously implementing a custom interface like databaseInterface. Your proposed syntax:

class database implements databaseInterface extends mysqli { 
    // etc
}

triggers a fatal error because PHP expects the inheritance clause (extends) to precede the implementation clause (implements). The parser processes the class definition from left to right, and it encounters implements before seeing extends, which contradicts the language grammar. This is why you see the message unexpected T_EXTENDS, expecting '{'.

Correct Syntax for Multiple Inheritance Sources

To resolve this, you must always declare the parent class first, followed by the interfaces. The correct syntax for your specific use case looks like this:

class database extends mysqli implements databaseInterface {
    public function query($q) {
        // Implementation logic
    }

    public function escape($s) {
        return $this->escape_string($s);
    }
}

By moving extends mysqli to the left of implements databaseInterface, you satisfy the parser's expectations. This pattern is not unique to this specific combination; it applies universally in PHP. If a class extends another class, any number of interfaces must follow immediately after. Conversely, if a class implements multiple interfaces without extending another class, the implements keyword comes first.

Managing Constructor Calls with parent

One of the most common pitfalls when combining extends and implements lies in the constructor logic. The mysqli class has a specific signature for its constructor that accepts connection parameters (host, username, password, database). If you do not call the parent constructor explicitly, the connection will fail silently or throw a fatal error later when you attempt to query the database.

Since your databaseInterface likely defines methods like query() and escape(), your constructor should initialize the connection before defining these behaviors. You must invoke parent::__construct() within your class body:

class database extends mysqli implements databaseInterface {
    public function __construct($host = 'localhost', $user, $pass, $db) {
        parent::__construct($host, $user, $pass, null, $db);
        // Initialize connection status checks here if needed
    }

    public function query($q) {
        return $this->query($q);
    }

    public function escape($s) {
        // Custom escaping logic
        return htmlspecialchars($s, ENT_QUOTES, 'UTF-8');
    }
}

Note that in the example above, we assume standard mysqli usage. If you are using PHP 7.3 or later, you might consider using typed properties for the connection object if you decide to refactor away from inheritance entirely, but sticking with extends mysqli is valid and often preferred for its built-in error handling capabilities.

Trade-offs and Design Considerations

While syntactically allowed, extending framework classes like mysqli or Laravel's PDO while implementing custom interfaces introduces specific architectural trade-offs. The primary benefit here is leveraging the robust features of the parent class—such as prepared statement support, transaction management, and error codes—without rewriting that logic.

However, you must ensure that your interface definition does not conflict with the parent class's method signatures. For instance, if mysqli already defines a query() method, your implementation in the child class can override it, but you cannot change the return type or argument count without adhering to Liskov Substitution Principle constraints. If your interface demands a different signature than what mysqli provides, PHP will not stop you at compile time, but runtime failures are inevitable when calling methods that expect specific behaviors from the parent class.

Additionally, consider whether this level of coupling is necessary. In modern PHP applications using dependency injection containers (like Laravel), it is often cleaner to have a base repository class that implements your interface and accepts a mysqli or PDO instance via constructor injection rather than extending the database driver directly. This decouples your business logic from specific database drivers, making testing significantly easier.

For further resources on building scalable PHP applications and architectural patterns, you might explore https://laravelcompany.com, which offers insights into modern framework usage and best practices.

Our network