PHP 接口实现拒绝参数的子类

发布于 2024-10-13 20:42:58 字数 326 浏览 12 评论 0原文

考虑一下:

class A{}

class B extends A{}

interface I{
 // expects object instanceof A
 function doSomething(A $a);
}

class C implements I
{
 // fails ????
 function doSomething(B $b){}
}

在我的构想中,上述内容应该有效,但它不会,因为 php 拒绝要求第一个参数与接口 (I) 中定义的类型 (A) 完全相同的实现。由于 B 是 A 的子类,所以我不明白有什么问题。我在这里错过了什么吗?

consider this:

class A{}

class B extends A{}

interface I{
 // expects object instanceof A
 function doSomething(A $a);
}

class C implements I
{
 // fails ????
 function doSomething(B $b){}
}

In my conception the above should work but it doesn't as php rejects that implementation requiring the first parameter to be exactly the same type (A) as defined in the interface (I). Since B is a subclass of A, I don't see whats the problem. Am I missing something here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖公主 2024-10-20 20:42:58

class C Implements I 意味着CI 之间必须存在子类型关系。这意味着 C 类型的对象应该可以在任何需要 I 类型的对象的地方使用。

在您的情况下,CI更具限制性,因为它对其doSomething参数有更精确的要求 - I.doSomething code> 适用于任何 A,但 C.doSomething 需要 A 的特定子类型

请注意,如果您更改 C.doSomething< /code> 接受任何 A 那么就没有什么可以阻止你向它传递一个 B 类型的对象。您不能只需要 B,因为那样您就会破坏子类型约定。

理论上,子类型可以对其函数参数更加自由,对其返回类型更加具体(但反之亦然,就像您的情况一样)。在实践中,编程语言可能要求重写方法中的参数类型必须在各处相同。

class C implements I means that there must be subtype relation between C and I. It means object of type C should be usable wherever an object of type I is required.

In your case C is more restrictive than I because it has more precise requirements on its doSomething argument -- I.doSomething is fine with any A but C.doSomething requires a specific subtype of A

Note that if you change C.doSomething to accept any A then nothing prevents you to pass it an object of type B. You just can't require only B, because then you would break subtyping contract.

In theory, subtypes can be more liberal about their function arguments and more specific about their return types (but never vice versa, as it was in your case). In practice, a programming language may require that argument types in overridden methods must be same everywhere.

掌心的温暖 2024-10-20 20:42:58

理论上,子类型对其函数参数可以更加自由
并更具体地说明它们的返回类型(但反之亦然,因为
这是你的情况)。在实践中,编程语言可能需要
重写方法中的参数类型必须在各处相同。

几乎没有什么办法 - instanceof 来解决这个问题:

class A{}

class B extends A{}

interface I{
 // expects object instanceof A
 function doSomething(A $a);
}

class C implements I
{
 
 function doSomething(A $b){
   if($b instanceof of B){
   //do something
   }else{throw new InvalidArgumentException("arg must be instance of B") };
 }
}

In theory, subtypes can be more liberal about their function arguments
and more specific about their return types (but never vice versa, as
it was in your case). In practice, a programming language may require
that argument types in overridden methods must be same everywhere.

little work around- instanceof to solve that problem:

class A{}

class B extends A{}

interface I{
 // expects object instanceof A
 function doSomething(A $a);
}

class C implements I
{
 
 function doSomething(A $b){
   if($b instanceof of B){
   //do something
   }else{throw new InvalidArgumentException("arg must be instance of B") };
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文