Take the State of PHP 2026 Survey →

Parameter Type Widening View as Markdown

Implemented (PHP 7.2)
Authors
Niklas Keller me@kelunik.com
Date
2017-01-03
Version
0.3
Source
wiki.php.net/rfc/parameter-no-type-variance

Vote

Parameter Type Widening closed
Yes
24
No
5
29 voters
ajf Yes 2017-01-09 20:29
ashnazg Yes 2017-01-13 15:01
bishop Yes 2017-01-12 16:49
bwoebi Yes 2017-01-11 13:49
colinodell Yes 2017-01-11 14:32
danack Yes 2017-01-17 14:09
daverandom Yes 2017-01-11 11:41
derick Yes 2017-01-10 13:52
dm Yes 2017-01-18 09:20
frozenfire Yes 2017-01-26 16:54
galvao No 2017-01-12 19:24
guilhermeblanco Yes 2017-01-13 16:50
hywan No 2017-01-10 12:39
jhdxr No 2017-01-11 15:58
jwage Yes 2017-01-15 15:16
lcobucci Yes 2017-01-29 22:08
leigh Yes 2017-01-17 21:28
levim No 2017-01-16 17:35
marcio Yes 2017-01-16 20:01
mariano Yes 2017-01-11 15:23
mike Yes 2017-01-10 08:54
nikic Yes 2017-01-15 14:17
ocramius Yes 2017-01-10 05:36
pollita Yes 2017-01-17 22:35
rdohms Yes 2017-01-19 13:04
sammyk Yes 2017-01-27 18:32
stas Yes 2017-01-11 18:58
trowski Yes 2017-01-12 17:50
zimt No 2017-01-25 13:22

Discussions on the mailing list

[RFC] Parameter No Type Variance
by Niklas Keller · 13 messages
[RFC][VOTE] Parameter Type Widening (Resume)
by Niklas Keller · 1 message
Contravariance and the "empty type"
by Andreas Hennings · 5 messages
Adding return types to internal methods (PHP 8)
by Nikita Popov · 9 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Proposal

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.

Example

<?php
 
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

None.

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.

Voting

Requires a 2/3 majority.

Vote: Parameter Type Widening — Yes: 24 · No: 5 (closed)

Voting started on 9th of January but has been stopped due to the mailing list failures. Voting has been resumed on 11th of January and has ended on 31th of January.

Patches and Tests

Implementation

  1. Merged into PHP 7.2
  2. https://github.com/php/php-src/commit/2edc3cf8d2dd75501bf1049cf1b3ca57f11f1234