dtlygweb2017 2018-02-08 15:36
浏览 19

如果父函数的参数多于子函数,则扩展函数不再有效,即使额外参数具有默认值也是如此

I've noticed a problem since PHP 7.2 whereby extending a function no longer works if the parent function has more arguments than the child, even if extra arguments in the parent class have default values.

The following fatal error is thrown:

Compile Error: Declaration of
WebBox\PageBundle\Form\Type\PageType::buildForm(Symfony\Component\Form\FormBuilderInterface $builder, array $options)
must be compatible with
WebBox\FrontBundle\Form\Type\CoreType::buildForm(Symfony\Component\Form\FormBuilderInterface $builder, array $options, $noSubmit = false, $submitText = NULL)

This is for a Symfony project whereby a form class (in this case PageType) extends from a shared form class CoreType, so many of our forms can get certain common functionality. -- This problem isn't limited to just the PageType class, it's just the first example we noticed.

Class CoreType defines a public function buildForm as:

public function buildForm(FormBuilderInterface $builder, array $options, $noSubmit = false, $submitText = null) {...}

Class PageType (and any others extending from CoreType) define buildForm as:

public function buildForm(FormBuilderInterface $builder, array $options) {...}

Function PageType::buildForm() (and others) will then typically call CoreType::buildForm() to finalise the form building with some common behaviour, optionally choosing to not put in a submit button ($noSubmit), or adjusting the submit button text for various reasons ($submitText). Examples:

parent::buildForm($builder, $options);
parent::buildForm($builder, $options, true);
parent::buildForm($builder, $options, false, $this->translate("Submit for approval"));

This has always worked from PHP 5.5 through to 7.1, but we're suddenly finding this is not allowed in PHP 7.2. (Can't see any mention of such a change in the 7.1 to 7.2 migration docs.)

Have we been doing this wrong for multiple years now, or is this a bug/deprecation in PHP 7.2?

This isn't a major deal for us as we can easily switch PHP version, but I appreciate for certain set-ups (e.g. Linux with package managers) this isn't so easy.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败