|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-08-17 17:31 UTC] 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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 08:00:02 2025 UTC |
Hi Derek, Thank you for your responses; I ran into this same issue. I hate to resurrect old tickets, but are you sure you want to leave this out? Here is my concern... There is no point to have type hinting if object inheritance is not respected. For example, let's say I have the following interface... interface Comparable { public function gte(Comparable $value); public function lte(Comparable $value); public function eq(Comparable $value); public function gt(Comparable $value); public function lt(Comparable $value); } and now I have the following base classes... class Orange implements Comparable { ... public function gte(Comparable $orange) { ... } public function lte(Comparable $orange) { ... } public function eq(Comparable $orange) { ... } public function gt(Comparable $orange) { ... } public function lt(Comparable $orange) { ... } } class Apple implements Comparable { ... public function gte(Comparable $apple) { ... } public function lte(Comparable $apple) { ... } public function eq(Comparable $apple) { ... } public function gt(Comparable $apple) { ... } public function lt(Comparable $apple) { ... } } Does it REALLY make sense to compare Apples to Oranges??? Kind regards, JohnHere's an example that shows, why this is not allowed: <?php class AnotherBar implements IBar { function getMessage() { } } function moo(Foo $foo) { $bar = new AnotherBar(); $foo->saySomethingElse($bar); } moo(new BarDAO()); This would be a syntactically valid code, but it would fail, because BarDAO::saySomethingElse() does not allow AnotherBar. It is complicated, but it is easier to understand, if you see an example.