Req #46705 [Com]: Impossible to implement compatible interfaces
| From: | tklingenberg at lastflood dot net | Date: | Sun, 27 Nov 2011 18:03:03 +0000 |
| Subject: | Req #46705 [Com]: Impossible to implement compatible interfaces | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-165523@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=46705&edit=1
ID: 46705
Comment by: tklingenberg at lastflood dot net
Reported by: wrzasq at gmail dot com
Summary: Impossible to implement compatible interfaces
Status: Open
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: Debian GNU/Linux
PHP Version: 5.2.6
Block user comment: N
Private report: N
New Comment:
Actually it's possible, you just need to make the interfaces compatible for
that:
interface A
{
public function foo();
}
interface B extends A
{
}
and then you're done. This also prevents you from writing duplicate code ;)
However if interfaces definitions can not be changed, then this is not a
solution. Probably this is fixed? - See bug #43200.
Previous Comments:
------------------------------------------------------------------------
[2011-08-01 12:11:11] smith at pooteeweet dot org
Note its also not possible to redefine while adding new optional parameters
<?php
interface fooI
{
function ding();
}
interface barI extends fooI
{
function ding($dong = null);
}
class bar implements barI
{
public function ding($dong = null)
{
echo 'woho: '.$dong."\n";
}
}
$bar = new bar;
$bar->ding('yeah!');
------------------------------------------------------------------------
[2011-03-22 23:33:03] clicky at erebot dot net
I think this use case, the one bug #43200 and the one below are all valid:
<?php
interface A
{
public function foo();
}
interface B
extends A
{
public function foo();
}
class C implements B
{
public function foo(){}
}
?>
The case above may seem odd (there's really no point in redefining the exact same interface),
but I have a simple use case where this may prove handy.
I'm currently writing some code for a little project which is meant to teach some middle to
advanced topics of PHP. The code is self-documented (using doxygen) and uses some interface from SPL
(Countable). That interface is used several times in different files and I'd like to document
the count() method only once (if possible, at the interface level -- then using doxygen's
ability to copy/paste the doc from parent classes/interfaces into the current class).
I thought I could just define my own interface (Project_Countable), extending from SPL's
Countable, "overriding" the method's signature (only so doxygen can pick up the
method's declaration -- the prototype for the method was actually left unchanged) and then
define a class that implements Project_Countable.
So in my case, A = SPL's Countable interface and B = Project_Countable.
However, this pattern can't be used as it results in the same error others noted here and in
bug #43200.
I can't simply avoid Project_Countable extending from Countable, because then I would lose
count()'s "magic" by doing so.
------------------------------------------------------------------------
[2008-11-28 07:57:42] wrzasq at gmail dot com
Description:
------------
It is impossible to implement multiple interfaces that declares same
methods. I saw bug #43200 but this is a bit different case and I
think in this situation it shoud be allowed - those interfaces
requires methods with same signature (also to prevent further
problems - optional parameters should also be allowed to match
signatures):
Reproduce code:
---------------
<?php
interface A
{
public function foo();
}
interface B
{
public function foo();
}
class C implements A, B
{
public function foo(){}
}
Expected result:
----------------
nothing, but working
Actual result:
--------------
Fatal error: Can't inherit abstract function B::foo() (previously
declared abstract in A) in /home/wrzasq/Desktop/Www/engine/- on line
16
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=46705&edit=1