【发布时间】:2013-04-10 16:21:42
【问题描述】:
我正在尝试实现类似于存储库模式的东西。为此,我有一个所有 repos 都实现的接口和一个所有模型都扩展的模型基类。
我从 PHP 收到以下错误消息
致命错误:MVCBP\Repositories\UserRepository::add() 的声明必须与 D:\wamp\www\mvc\MVCBP 中的 MVCBP\Core\RepositoryInterface::add(MVCBP\Core\Model $model) 兼容\Repositories\UserRepository.php 第 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
}
模型接口.php
<?php
namespace MVCBP\Core;
interface ModelInterface {}
用户.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
}
【问题讨论】:
-
你为什么认为模型是一个类?
标签: php oop inheritance polymorphism