Bug #42330 [Com]: Type hinting based upon interfaces not working

From: Date: Thu, 16 Aug 2012 14:15:28 +0000
Subject: Bug #42330 [Com]: Type hinting based upon interfaces not working
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-172639@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=42330&edit=1

 ID:                 42330
 Comment by:         jsuprock at gmail dot com
 Reported by:        udo dot rader at bestsolution dot at
 Summary:            Type hinting based upon interfaces not working
 Status:             Wont fix
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Linux
 PHP Version:        5.2.3
 Block user comment: N
 Private report:     N

 New Comment:

Derick,

  I'm sorry I misspelled your name.

Sincerely,

John S


Previous Comments:
------------------------------------------------------------------------
[2012-08-16 14:13:47] jsuprock at gmail dot com

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,

John

------------------------------------------------------------------------
[2007-08-17 18:16:37] udo dot rader at bestsolution dot at

Hmm, too bad.

The link you provided about the PDM meeting says "that most probably no language currently
implements this correctly" is not true. 

Java for instance does this just nicely at least since 1.3.

With that limitation, the implementation of an AbstractDAO/AbstractFactory pattern will fail if type
hinting is wanted, because transfer objects need to be identifyable by their common interface.
Otherwise the entire interface thing in PHP does not make much sense, IMHO.

So anyhow, at least the error message should be changed to be more clear.

------------------------------------------------------------------------
[2007-08-17 17:47:13] derick@php.net

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

------------------------------------------------------------------------
[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 https://bugs.php.net/bug.php?id=42330&edit=1


Thread (6 messages)

« previous php.bugs (#172639) next »