我試圖實現類似於存儲庫模式的東西。爲此,我有一個所有Repos實現的接口和一個所有模型擴展的模型基類。如何在接口中使用多態性?
我從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
以下錯誤消息我想的是,我在倉庫類中的方法應根據接口接受模型的實例作爲參數。不過,我想在實現中鍵入提示特定的模型。這在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
}
爲什麼你認爲模型是一類? –