English 中文(简体)
PHP OOP “执行必须相容”
原标题:PHP OOP "Implementation must be compatible" [duplicate]
  • 时间:2012-01-14 14:34:28
  •  标签:
  • php
  • oop

I m在PHP OOP和打字时遇到麻烦。 例如:

abstract class AC {}

class C extends AC {}

interface I {
    function method(AC $abstract);
}

class InterfaceImplementation implements I {
    function method(C $concrete) {}
}

这部法典赢得了引书,指出method与接口声明不一致。 我认为,这是符合的,因为C延伸AC——我是否错了? 预计我将如何执行这种职能?

最佳回答

缩略语 B,其中也包括<代码>AC。 然后,I要求其实施中的任何一项也接受<条码>B作为方法的理由。 页: 1

The bigger picture: You might want to reconsider your design if you need to specify a concrete type in the implementation. The idea is that to the outside world, all that needs to be known is encoded by AC and there should not be an InterfaceImplementation that ever needs to know which concrete subclass is being transferred. Maybe the specific stuff can be embedded in C s code and generically called through a method exposed by AC?

另一项更新: 你们也许能够利用通用术语来完成你们想要的东西,但我并不认为这些东西存在于购买力平价中。 我仍然想到,你是否分享设计问题的细节,这可能造成另一个令人感兴趣的问题:

问题回答

仅将其定义为:

class InterfaceImplementation implements I {
    function method(AC $concrete) {}
}

并以C为例。 ->method(new C());

The PHP reference Manual on 目标接口 明确阐明:

执行接口的类别必须使用接口中界定的完全相同的方法签名。 不这样做会造成致命错误。





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签