【发布时间】:2013-02-19 07:16:04
【问题描述】:
这样的代码:
interface entite
{
}
class Foo implements entite
{
}
$foo = new foo;
if( $foo instanceof entite ) echo "he is";
显示“他是”。 Foo 从接口继承类型“entite” 但是当尝试这样做时:
class FooDeleter implements deleter
{
public function __construct(Foo $Foo)
{
}
}
interface deleter
{
public function __construct(entite $entite);
}
给我:
Fatal error: Declaration of FooDeleter::__construct() must be compatible with deleter::__construct(entite $entite)
为什么?如何 ? =(
编辑:独特的方式实际上是这样定义类型删除器:
class FooDeleter implements deleter
{
public function __construct(entite $Foo)
{
if( $Foo instanceof Foo ) { ... }
}
}
【问题讨论】:
-
希望你的意思是“OOP 接口”?
-
这是来自 PHP Big Brown Book 的示例代码。
-
我很惊讶 PHP 允许您在接口中定义构造函数。对我来说似乎是一个可怕的想法。相关问题在这里 - stackoverflow.com/questions/783046/…
-
@Phil 这确实看起来像是一个无用的功能。
-
Actor 是一个实现细节,不应进入接口。
标签: php oop interface inheritance