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.