|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2019-11-15 13:13 UTC] girgias@php.net
-Status: Open
+Status: Not a bug
[2019-11-15 13:13 UTC] girgias@php.net
[2019-11-15 13:40 UTC] me at paveljanda dot com
[2019-11-15 13:51 UTC] requinix@php.net
-Package: PHP Language Specification
+Package: *General Issues
[2019-11-15 13:51 UTC] requinix@php.net
[2019-11-15 14:15 UTC] me at paveljanda dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 13 20:00:01 2025 UTC |
Description: ------------ 1, Liskov substitution principle: interface FuelType { } class Gas implements FuelType { public function burn(int $litres) {} }; class Battery implements FuelType { public function discharge(int $kWh) {} }; class Vehicle { public function drive(Gas $fuel) { $battery->burn(10); } } class ElectricVehicle extends Vehicle { public function drive(FuelType $battery) { // do something } } (new ElectricVehicle)->drive(new Battery); -> This code works fine in 7.4 RC6. I don't think it should. That code would enable ElectricVehicle::drive() to accept "smaller" argument with less functionality that the "bigger" Gas class that is required by parent (!!) class method. 2. In the example above, when I change the ElectricVehicle into following form: class ElectricVehicle extends Vehicle { public function drive(Battery $battery) { $battery->discharge(10); } } , PHP tells me different result (Warning: Declaration of ElectricVehicle::drive(Battery $battery) should be compatible with Vehicle::drive(Gas $fuel)) event though Battery is a type of FuelType which worked fine in the previous example. It makes perfect sense to use Type variance in the opposite direction - classes that extend from Vehicle should be able to accept a class that extends Gas type. It should be OK to extends the successor behaviour. But it's not OK do make it both directions. Thank you all for spending some time with that report! Pavel Janda Test script: --------------- https://gist.github.com/paveljanda/cba6d31be920217c620289e54fed7c1c