#42330 [Opn->WFx]: Type hinting based upon interfaces not working

From: Date: Fri, 17 Aug 2007 17:47:14 +0000
Subject: #42330 [Opn->WFx]: Type hinting based upon interfaces not working
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-116258@lists.php.net to get a copy of this message
 ID:               42330
 Updated by:       derick@php.net
 Reported By:      udo dot rader at bestsolution dot at
-Status:           Open
+Status:           Wont fix
 Bug Type:         Scripting Engine problem
 Operating System: Linux
 PHP Version:      5.2.3
 New Comment:

PHP does not implement inheritance checks for typehinted arguments.
Please also refer to the PDM meeting notes, where this was discussed:
http://www.php.net/~derick/meeting-notes.html#implement-inheritance-rules-for-type-hints


Previous Comments:
------------------------------------------------------------------------

[2007-08-17 17:31:48] udo dot rader at bestsolution dot at

Description:
------------
When using type hinting with interfaces it fails to recognoize classes
implementing the interface as "compatible".

Reproduce code:
---------------
abstract class Foo  {
    abstract public function saySomething( IBar $what );
    abstract public function saySomethingElse( IBar $what );
}
class BarDAO extends Foo {
    function saySomething( IBar $what ) {
        echo $what->getMessage();
    }
    function saySomethingElse( BarTransfer $what ) {
        echo $what->getMessage();
    }
}
interface IBar {
    function getMessage();
}
class BarTransfer implements IBar {
    function getMessage() {
        return "Hello from BarTransfer";
    }
}
$barDAO = new BarDAO();
$barTransfer = new BarTransfer();
$barDAO->saySomething( $barTransfer );
$barDAO->saySomethingElse( $barTransfer );


Expected result:
----------------
The expected result should be that no compiler error occurs and that
the script runs.

Actual result:
--------------
The PHP interpreter gives this error:

Fatal error: Declaration of BarDAO::saySomethingElse() must be
compatible with that of Foo::saySomethingElse()

But, IMHO, BarTransfer can hardly be more "compatible" with IBar.



------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=42330&edit=1


Thread (6 messages)

« previous php.bugs (#116258) next »