#42330 [Opn->WFx]: Type hinting based upon interfaces not working
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)