Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

HenrijsS's avatar

How can I extend an interface to say it has all the functions of a model

Hello everyone.

So I have a model - InventoryItem, which implements a AGGridModelInterface. I do this, because I need specific columns and methods in InventoryItem that are used for the formatting AGGridService.

The problem is that, in the AGGridService, I only use the Interface, because it's Model agnostic, and it's screaming at me that, for example, where does not exist.

Using abstract classes instead of interfaces is weird to me. What would be the best option here?

In short: How can I extend an interface to say it has all the functions of a model?

0 likes
3 replies
Sergiu17's avatar
/**
 * @mixin \Illuminate\Database\Query\Builder
 */
interface AGGridModelInterface { }

I assume your IDE is screaming

1 like
martinbean's avatar

@henrijss This kinda defeats the point of using an interface in the first place.

An interface is meant to declare the functionality (methods) an object has, and you’re meant to code against those only. If you get an interface, but then go, “But this is X” then you’ve just made the interface completely redundant.

Either use the interface for its intended purpose, or actually declare in your interface all of the functionality you need to rely on in calling code.

1 like
Sergiu17's avatar

Another option, PHP 8.2 added support for DNF types https://wiki.php.net/rfc/dnf_types

use Illuminate\Database\Eloquent\Model;

class AGGridService {
  public function __construct(protected AGGridModelInterface&Model $grid) {
    //
  }
}

This will make sure that your class is subtype of AGGridModelInterface and Illuminate\Database\Eloquent\Model

Please or to participate in this conversation.