Bug #75095 [NEW]: Interface inheritance not allowed properly
| From: | adaliszk at gmail dot com | Date: | Sat, 19 Aug 2017 12:52:15 +0000 |
| Subject: | Bug #75095 [NEW]: Interface inheritance not allowed properly | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-210740@lists.php.net to get a copy of this message | ||
From: adaliszk at gmail dot com
Operating system: Irrelevant
PHP version: 7.1.8
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Interface inheritance not allowed properly
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.
--
Edit bug report at https://bugs.php.net/bug.php?id=75095&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=75095&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=75095&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=75095&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=75095&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=75095&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=75095&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=75095&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=75095&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=75095&r=support
Expected behavior: https://bugs.php.net/fix.php?id=75095&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=75095&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=75095&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=75095&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75095&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=75095&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=75095&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=75095&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75095&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=75095&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=75095&r=mysqlcfg