This is an old revision of the document!
PHP RFC: Parameter No Type Variance
- Version: 0.1.1
- Date: 2016-11-21
- Author: Niklas Keller me@kelunik.com
- Status: Under Discussion
- First Published at: http://wiki.php.net/rfc/parameter-no-type-variance
Introduction
PHP doesn't currently allow variance for parameters as checking these for compatibility isn't possible on compile time. This limitation is caused by autoloading and doesn't allow widening the accepted parameters.
Proposal
This RFC proposes to allow ommiting the type entirely in a subclass, as dropping all parameter constraints is always valid according to the LSP principle.
Example
<?php class ArrayClass { public function foo(array $foo) { /* ... */ } } class IterableClass extends ArrayClass { // This implementation also accepts Traversable. // Variance is already possible in this special case using "iterable". public function foo(iterable $foo) { /* ... */ } } class EverythingClass extends ArrayClass { // This implementation accepts all values. // Restrictions may be done via user code in the method body. // Variance is currently not allowed, it throws a warning if parent or child miss a type. 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 minor version, currently 7.2.
RFC Impact
To SAPIs
None.
To Existing Extensions
None.
To Opcache
None.
New Constants
None.
php.ini Defaults
None.
Open Issues
None.
Future Scope
In the future there might be support for contra-variance / co-variance for parameters / return types.
Proposed Voting Choices
Requires a 2/3 majority.
Patches and Tests
TBD.
Implementation
TBD.
References
None.
Rejected Features
None.