本文介绍了PHP:“...的声明应该与...的声明兼容";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为可 CRUDable(可以保存和删除)的实体创建一个界面.这是我的抽象类:

I'd like to create an interface for entities that are CRUDable (can be saved and deleted). Here is my abstract class:

abstract class AbstractCrudableEntity extends AbstractEntity
{
    abstract public function doSave();
    abstract public function doDelete();
}

我的实现类需要这些方法的几个额外参数.这是实现类的签名:

My implementing class needs a couple extra parameters to those methods. Here is the signature of the implementing class:

class Contact extends AbstractCrudableEntity {
    public function doSave(User $user, \UberClientManager $manager);
    public function doDelete(User $user, \UberClientManager $manager);
}

我理解PHP要求实现类的方法参数与父类相同(有几个问题可以回答这个问题:this,例如).所以这不是问题.

I understand that PHP requires that implementing classes have the same parameters for the methods as the parent class (there are several questions that answer this question: this, for example). So that is not the problem.

然而,我最近在 Symfony 中遇到了一些处理身份验证令牌的代码.类 UsernamePasswordToken 扩展了 AbstractToken,并且在 __construct() 方法中有一组不同的参数:AbstractToken::__construct()UsernamePasswordToken::__construct().

However, I recently came across some code in Symfony dealing with authentication tokens. The class UsernamePasswordToken extends AbstractToken, and has a different set of parameters in the __construct() method: AbstractToken::__construct() versus UsernamePasswordToken::__construct().

我的问题是 Symfony 是如何做到这一点的?这和我的代码有什么区别?

My question is how is Symfony able to do this? What is the difference between this and my code?

推荐答案

重写构造函数是一种特殊情况:

与其他方法不同,当__construct()被与父__construct()不同的参数覆盖时,PHP不会生成E_STRICT级别的错误消息 方法有.

你可以不能用其他方法做到这一点.

You can not do that with other methods.

这篇关于PHP:“...的声明应该与...的声明兼容";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 22:25