|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2017-08-19 13:06 UTC] nikic@php.net
-Status: Open
+Status: Not a bug
[2017-08-19 13:06 UTC] nikic@php.net
[2017-08-19 13:08 UTC] example at example dot org
[2017-08-19 13:40 UTC] adaliszk at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Dec 17 07:00:01 2025 UTC |
Description: ------------ It seems that the engine not using inheritance properly with paramether types in abstract classes (which would be solved in 7.2) and in interfaces. The engine seems not seeing that a sub-interface is compatible with the base interface it just may have more functionalities but the expected boundries are meet even if you override a function method with a sub-interface of the specified function paramether. You can bypass this to write the same code without using inheritance or checking the entity type inside your class: - The first bypass solution is just not great, since if your collection base got new functionalities or refactored for some reason, then you have to copy-paste trough all of your collection interfaces, it's not efficient. - The second bypass solution is making a joke from the engine typecheck features, since it has a power to check the input types, but you cannot use it and you have to write typecheck manually which way you have to write more code. It would be great if the engine allow us to override with compatible types the method signitures like it will allow it with abstract classes and interfaces are abstract classes with only abstract methods in it. Test script: --------------- An example for this: Create a Collection interface and Entity interface where Entities could add/remove to it. Create sub-interfaces with more functionalities and override the collection add/remove so it would only allow a sub-interface instead a globaly used one. // Base interfaces interface Entity { public function getId(): int; } interface Collection { public function add(Entity $item): void; public function remove(Entity $item): void; } // just more functionality interface ProductEntity extends Entity { public function getName(): string; } /// override the types with a subtype, which is compatible by LSP interface ProductCollection extends Collection { public function add(ProductEntity $item): void; public function remove(ProductEntity $item): void; } Expected result: ---------------- No errors because the sub-interfaces are compatible with the base interface using Liskov Substitute Principle. Actual result: -------------- Declaration are not compatible error.