[英]PHP type hint interface in abstract methods and type hint interface's child class in method implementation
Imagine I have a simple interface: 想象一下,我有一个简单的界面:
interface A {}
Then, I have some classes implementing that interface: 然后,我有一些实现该接口的类:
class B implements A {}
class C implements A {}
Then, I have a class which have a method which expects object of class B as an argument (I can not use interface type hint here because only class B have some unique features class D requires): 然后,我有一个类,它有一个方法,它希望类B的对象作为参数(我不能在这里使用接口类型提示,因为只有类B具有类D要求的一些独特的功能):
class D
{
public function foo(B $bar) { /*...*/ }
}
But, I also have another class with the same method which now expectes object of class C as an argument (I can not use interface type hint here because only class C have some unique features class E requires): 但是,我还有另一个类使用相同的方法,现在将类C的对象作为参数(我不能在这里使用接口类型提示,因为只有类C具有类E需要的一些独特的功能):
class E
{
public function foo(C $bar); { /*...*/ }
}
And objects of class D and E have to be used somewhere else and I have to make sure that these objects will have method foo
, so I ended up with the following (abstract class is not a mistake here, should not use interface instead, because in real life this class should contain some non-abstract methods too) : 并且D类和E类的对象必须在其他地方使用,我必须确保这些对象将具有方法foo
,所以我最终得到以下内容(抽象类在这里不是错误,不应该使用接口,因为在现实生活中,这个类也应该包含一些非抽象的方法) :
abstract class DE
{
abstract public function foo(A $bar); // type-hint interface
}
Then I edited my classes so they extend class DE: 然后我编辑了我的类,所以他们扩展了类DE:
class D extends DE
{
public function foo(B $bar); // class B implements A
}
class E extends DE
{
public function foo(C $bar); // class C implements A
}
To my mind this code is logically valid, because we may have situations I tried to describe in this post, but I was surprised to see Fatal error with the message : Declaration of .. must be compatible with .. 在我看来,这段代码在逻辑上是有效的,因为我们可能会遇到我试图在这篇文章中描述的情况,但我很惊讶地看到致命错误的消息: 声明......必须与...兼容。
So my question is the following: How to make sure that several objects will have a specific method and that method in each object will accept specific objects only as an argument? 所以我的问题如下: 如何确保多个对象具有特定方法,并且每个对象中的方法仅接受特定对象作为参数?
As of now, the only workaround I see is something like the following ( but I believe there is a better solution for this ): 截至目前,我看到的唯一解决方法是以下内容( 但我相信有更好的解决方案 ):
interface A {}
class B implements A {}
class C implements A {}
interface DE
{
public function foo(A $bar);
}
class D implements DE
{
public function foo(A $bar)
{
assert($bar instanceof B);
/*...*/
}
}
class E implements DE
{
public function foo(A $bar)
{
assert($bar instanceof C);
/*...*/
}
}
One problem is classes D and E requiring concrete classes, B and C respectively. 一个问题是D类和E类分别需要具体的B类和C类。 Now since you should be discouraging class inheritance I would avoid putting concrete classes in method signatures as much as I can. 既然你应该劝阻类继承,我会尽量避免在方法签名中放入具体的类。 If your class needs to use a concrete class don't leak that information, keep it private. 如果您的类需要使用具体类,请不要泄漏该信息,请将其保密。
So it is better to have: 所以最好有:
public function foo(SomethingAbstract $noIdeaWhatIAmButIDoStuff) {
$concrete = SomethingConcreteThatMakesUseOfAbstract($noIdeaWhatIAmButIDoStuff);
}
than: 比:
public function foo(SomethingConcrete $tmi) {
}
I would recommend to think along these lines: 我建议按照以下思路思考:
abstract class DE {
public abstract function foo(A $a);
}
class D extends DE {
public function foo(A $a) {
$b = new B($a);
//do with $b what you please
}
}
class E extends DE {
public function foo(A $a) {
$c = new C($a);
//now you can use $c which will leverage $a
}
}
If B and C objects need also to implement A (like in your example) then you have a decorator pattern. 如果B和C对象也需要实现A(就像你的例子中那样),那么你就有了一个装饰器模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.