I'm using the repository pattern to fetch my data from the database.
Here is my base class:
class BaseRepository implements EloquentRepositoryInterface
{
protected $model;
public function __construct(Model $model)
{
$this->model = $model;
}
public function findOrFail($id): Model
{
try {
return $this->model->findOrFail($id);
} catch (\Exception $e) {
throw new \Exception($e);
}
}
}
which implements this interface:
interface EloquentRepositoryInterface
{
public function findOrFail($id): Model;
}
Then I have one entity calledItem
, which extendsModel
:
class Item extends Model
{
// ...
}
And what I want to do is create an ItemRepositoryInterface:
interface ItemRepositoryInterface extends EloquentRepositoryInterface
{
public function findOrFail($id): Item;
}
But I can't change the interface signature... PhpStorm is telling me that is incompatible so I had to remove thepublic function findOrFail($id): Item
from the interface. So that my$itemRepository->findOrFail()
respects the signature ofEloquentRepositoryInterface
.
Here my ItemRepository :
class ItemRepository extends BaseRepository implements ItemRepositoryInterface
{
// ...
}
is that when I use$itemRepository->findOrFail()
the specs tells me it's returning aModel
is that when I call$itemRepository->findOrFail()
the specs should tell me that it's returning anItem
Is there a way to have this behaviour ? Like keeping the signature offindOrFail()
inside EloquentRepositoryInterface and 'overwrite' the return type of it, without having to rewrite the whole function ?
Why do you want to use the BaseRepository at all? The solution for your problem would be generics but as far as i know php doesn't ship with support for generics. You have 3 options: you could either remove type information from method contract for the return type or skip that BaseRepository. Or you don't use repositories at all with eloquent, since eloquent uses the Active Record Pattern, your persistence will always be bound to your models. So having an additional Repository layer which delegates everything to the model looks a bit overhead, except you want to abstract some complex queries. If you still wanna use Repositories, then go for the second option.
interface ItemRepository {
public function findOrFail($id) : Item;
}
class EloquentItemRepository implements ItemRepository
{
public function findOrFail($id) : Item {
return Item::findOrFail($id);
}
}
I don't believe you can "override"/rewrite the return type of an interface method on PHP. The php documentation on return types states the "return type during inheritance is invariant":
The enforcement of the declared return type during inheritance is invariant; this means that when a sub-type overrides a parent method then the return type of the child must exactly match the parent and may not be omitted. If the parent does not declare a return type then the child is allowed to declare one.
One possible solution will be to create a second method with a different name on theItemRepositoryInterface
that would call and covert the result offindOrFail
:
interface ItemRepositoryInterface extends EloquentRepositoryInterface
{
public function findOrFailItem($id): Item;
}
EloquentRepositoryInterface:
interface EloquentRepositoryInterface
{
public function findOrFail($id): ?Model;
}
The syntax ?Model will return model if it exists. Use the same for the item interface
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.