PHP接口(interface): specify a parameter list,但不是参数类型

标签 php interface parameters

我创建了一个名为iMapper 的界面。我希望我所有的映射器文件都实现该接口(interface)。

但是每个mapper都会指定参数类型。

例子:

interface iMapper
{
    public function insert($obj);
    public function update($obj);
    public function delete($obj);
}
class CarMapper implements iMapper
{
    public function insert(Car $obj){}
    public function update(Car $obj){}
    public function delete(Car $obj){}
}

该代码生成以下错误:

Declaration of CarMapper::insert() must be compatible with that of iMapper::insert()

它们是否是一种使界面与 CarMapper 兼容的方法? (我不想更改映射器。)

谢谢

最佳答案

“但是每个映射器都会指定参数类型。” - 我不得不说这是不可能的。

必须实现接口(interface)。这是什么意思?所有实现类都必须能够使用带有未指定参数的方法 - 接口(interface)内部方法所需的参数。

在方法体内调用instanceof是一种出路,但确实不是什么好办法。

阅读策略模式,我打赌它可以解决您的问题- http://sourcemaking.com/design_patterns/strategy/php

关于PHP接口(interface): specify a parameter list,但不是参数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5736199/

相关文章:

java为另一个接口(interface)创建一个包装接口(interface)

delphi - 接口(interface)、匿名方法和内存泄漏

loops - 如何在 Postman 中使用不同的客户对象数据循环 JSON 请求?

mysql - 如何在 MySQL 中创建一个字段,从 Django 中的可调用函数获取数据?

php - Uncaught Error : Undefined class constant 'ERRMODE_EXPECTION'

php - 登录系统,每次重新加载时都会将您注销

php - 在 Joomla 管理员中使用 JRoute::_()

C# HttpWebRequest POST 不发送参数

java - 如何使用 pom.xml 将参数传递给 Maven 构建?

php file_get_contents() 显示页面而不是 html 源代码