草庐IT

INTERFACE

全部标签

php - 将接口(interface)作为函数参数(PHP)传递?

我正在观看有关编码规则的JeffsLaracast教程之一。functionsignUp($subscription){if($subscription=='monthly'){$this->createMonthlySubscription();}elseif($subscription=='forever'){$this->createForeverSubscription();}}他想在这里使用多态和接口(interface)。他将上面的代码修改为:functionsignUp(Subscription$subscription){$subscription->create();

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

考虑一下:classA{}classBextendsA{}interfaceI{//expectsobjectinstanceofAfunctiondoSomething(A$a);}classCimplementsI{//fails????functiondoSomething(B$b){}}在我的概念中,上面应该工作,但它不作为php拒绝要求第一个参数与接口(interface)(I)中定义的类型(A)完全相同的实现。由于B是A的子类,所以我看不出有什么问题。我在这里遗漏了什么吗? 最佳答案 classCimplementsI意

php - 依赖注入(inject)——传一个完整的类好还是传一个类名好?

对于依赖注入(inject),我知道我必须将一个类的实例传递给主实例而不是主类创建它自己的实例,就像这样(php):classClass_One{protected$_other;publicfunctionsetOtherClass(An_Interface$other_class){$this->_other_class=$other_class;}publicfunctiondoWhateverYouHaveToDoWithTheOtherClass(){$this->_other_class->doYourThing();}}interfaceAn_Interface{publ

php - 在接口(interface)实现中覆盖参数类型

假设我有一个模型接口(interface):interfaceModel{}接口(interface)中的一个函数接收该模型作为参数:interfaceService{publicfunctionadd(Model$model);}为什么,当我使用另一个实现上述服务的模型来实现该服务时:classAnotherModelimplementsModel{}classAnotherServiceimplementsService{publicfunctionadd(AnotherModel$model);}我收到这个错误:Fatalerror:DeclarationofAnotherSer

php - 完全合格的接口(interface)名称

有没有办法获得类似于MyClass::class的完全限定接口(interface)名称?例如:namespaceExample\Tests;useExample\Interfaces\InputInterface;...classCommandTest......publicfunctioncreateInputMock(){//IwanttoreplacenextstringwithsomethingsimilartoMyClass::class$this->getMockBuilder('Example\Interfaces\InputInterface')...谢谢。

php - PHP 中 ArrayAccess 接口(interface)的好处?

在PHP中实现ArrayAccess接口(interface),我们可以访问对象属性作为数组键。让对象的行为像数组有什么好处?就像我看到使用ArrayAccessInterface实现“FORM”的框架一样然后我们可以访问(HTML)表单对象字段,例如,$form['nameField']instead-of$form->nameField$form['titleField']instead-of$form->titleField使用$form['nameField]代替$form->nameField有什么好处?是“数组数据结构”的速度还是对象和数组形式之间的可移植性?或者我错过了什

php - 从继承接口(interface)的类调用静态方法

这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Referringtoastaticmemberofasubclass请查看以下代码以了解我的问题。有没有办法强制抽象returnSomething(),同时保持从抽象“中间人”类中定义的函数调用函数的可能性?对我来说,这似乎是PHP的瓶颈。

php - 接口(interface)和抽象类的优点是什么?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicates:purposeofinterfaceinclassesWhatisthedifferencebetweenaninterfaceandabstractclass?大家好,我是一名PHP程序员。任何人都可以解释使用接口(interface)和抽象类的优势是什么。

类实现接口(interface)时PHP Trait冲突

我有一个实现execute方法接口(interface)的类。接口(interface)强制execute方法有两个带有一些类型提示的参数。我还使用了一个具有execute方法的特征,但具有不同的功能和签名。我通过使用更改特征方法名称:classMyClassimplementsMyInterface{useMyTrait{executeasprotectedcommanderExecute;}publicfunctionexecute(SomeInterface$arg1,SomeInterface2$arg2){//dosomething}}当我尝试运行该应用程序时,它会抛出一个f

php - 类不返回接口(interface)实例 PHP

我在实现接口(interface)的类上遇到奇怪的错误。错误:Catchablefatalerror:Argument1passedtoMyApp\Library\Cache::__construct()mustbeaninstanceofMyApp\Contacts\CacheInterface,instanceofMyApp\Driver\Cache\Filegiven文件类别:namespaceMyApp\Driver\Cache;useMyApp\Library\Config;useMyApp\Contracts\CacheInterface;classFileimplemen