I'm attempting to implement something that sort of resemble the the repository pattern. To do this I have a interface that all repos implement and a model base class that all models extend.
I get the following error message from PHP
Fatal error: Declaration of MVCBP\Repositories\UserRepository::add() must be compatible with MVCBP\Core\RepositoryInterface::add(MVCBP\Core\Model $model) in D:\wamp\www\mvc\MVCBP\Repositories\UserRepository.php on line 9
What I want is that my methods in the repository classes should accept an instance of Model as argument according to the Interface. However I want to type hint a specific Model in the implementation. Is this doable in PHP?
RepositoryInterface.php
<?php
namespace MVCBP\Core;
use MVCBP\Core\ModelInterface;
interface RepositoryInterface
{
public function add(ModelInterface $model);
}
UserRepository.php
<?php
namespace MVCBP\Repositories;
use MVCBP\Core\PDOMySQL;
use MVCBP\Core\RepositoryInterface;
use MVCBP\Models\User;
class UserRepository extends PDOMySQL implements RepositoryInterface
{
public function add(User $user)
{
//Omitted
}
//Omitted
}
ModelInterface.php
<?php
namespace MVCBP\Core;
interface ModelInterface {}
User.php
<?php
namespace MVCBP\Models;
use MVCBP\Core\ModelInterface;
use MVCBP\Core\Validate;
use MVCBP\Repositories\UserRepository;
require_once(__DIR__ . '/../lib/password.php');
class User implements ModelInterface
{
//Omitted
}