Niklas Keller


PHP RFC: Parameter Type Widening


PHP currently doesn't allow any variance of parameter types when checking whether a method implemented in a class is compatible with the method defined either in the parent class or interface.

This RFC proposes to allow omitting a type entirely in a subclass, as dropping all parameter constraints is always valid according to the contravariance rule.

Implementing this RFC would allow libraries to be upgraded to use type declarations in their method signatures. Currently adding types to a method of a class in a library would break any code that extends that class.

This would provide an easier upgrade path for libraries to start using scalar types, to replace manual checks being done inside the methods, without requiring an update for all sub-classes.

Another example of this being useful is DateTime::createFromFormat(). This method is already documented to accept only a class of type DateTimeZone as the 3rd parameter in the manual, but the type declaration is not actually present in the implementation.

A change was made to the DateTime::createFromFormat() to add the DateTimeZone type to the 3rd parameter. However, this change had to be reverted, as all classes extending DateTime currently don't have this type declaration, so they started throwing a method signature mismatch warning. This RFC would allow the DateTimeZone type to be added to the 3rd parameter, without breaking code that extends the DateTime class.


class ArrayClass {
  public function foo(array $foo) { /* ... */ }
// This RFC proposes allowing the type to be widened to be untyped aka any
// type can be passed as the parameter.
// Any type restrictions can be done via user code in the method body.
class EverythingClass extends ArrayClass {
  public function foo($foo) { /* ... */ }

Current Result

Warning: Declaration of EverythingClass::foo($foo) should be compatible with ArrayClass::foo(array $foo) in %s on line 18

Proposed Result

Compiles without a warning.

Backward Incompatible Changes


Proposed PHP Version(s)

Next version, currently 7.2.

Future Scope

Unfortunately “true” contravariance for class types isn't part of this RFC, as implementing that is far more difficult, and would require additional rules about autoloading and/or class compilation, which might only be acceptable at a major release.

Patches and Tests



An option needs 2/3 votes to win

Parameter Type Widening (82.8% approved)
User Vote
ajf Yes
ashnazg Yes
bishop Yes
bwoebi Yes
colinodell Yes
danack Yes
daverandom Yes
derick Yes
dm Yes
frozenfire Yes
galvao No
guilhermeblanco Yes
hywan No
jhdxr No
jwage Yes
lcobucci Yes
leigh Yes
levim No
marcio Yes
mariano Yes
mike Yes
nikic Yes
ocramius Yes
pollita Yes
rdohms Yes
sammyk Yes
stas Yes
trowski Yes
zimt No