php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60107 Allow subtype polymorphism in interfaces
Submitted: 2011-10-21 11:59 UTC Modified: 2016-04-16 14:10 UTC
Votes:2
Avg. Score:3.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: mkallman at gmail dot com Assigned:
Status: Wont fix Package: Unknown/Other Function
PHP Version: 5.3.8 OS: All
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mkallman at gmail dot com
New email:
PHP Version: OS:

 

 [2011-10-21 11:59 UTC] mkallman at gmail dot com
Description:
------------
Please allow for subtype polymorphism/downcasting in interfaces. 

Test script:
---------------
interface Deliverable {}

interface DeliverableWithTruck extends Deliverable {}

interface DeliveryService {
    function deliver( Deliverable $deliverable, $destination );
}


class CookieJar implements DeliverableWithTruck {}

class ExpressDeliveryService implements DeliveryService {

    function deliver( DeliverableWithTruck $deliverable, $destination ) {
        echo "Delivery underway";
    }

}


$fedEx = new ExpressDeliveryService;
$chocolateChipCookies = new CookieJar;

$fedEx->deliver( $chocolateChipCookies, "Sesame Street" );


Expected result:
----------------
Delivery underway

Actual result:
--------------
Fatal error: Declaration of ExpressDeliveryService::deliver() must be compatible 
with that of DeliveryService::deliver()

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-25 09:31 UTC] dagguh at gmail dot com
I disagree, your request violates the LSP. 
http://en.wikipedia.org/wiki/Liskov_substitution_principle

Derived classes cannot expect more than parent class expects.
By more I mean a condition that is harder to meet.
 [2016-04-16 14:10 UTC] nikic@php.net
-Status: Open +Status: Wont fix
 [2016-04-16 14:10 UTC] nikic@php.net
As dagguh says, this will not be supported as it violates LSP. If anything we'll only allow the reverse.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 17:01:30 2024 UTC