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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Wed May 07 03:01:27 2025 UTC