Edit report at http://bugs.php.net/bug.php?id=51542&edit=1
ID: 51542
Comment by: mike at mikegerwitz dot com
Reported by: mike at mikegerwitz dot com
Summary: Overriding method type hint with child interface
raises strict standards
Status: Bogus
Type: Bug
Package: Scripting Engine problem
Operating System: GNU/Linux
PHP Version: 5.3.2
New Comment:
Ah - seems you are correct. I must have tested improperly. I was not
aware that
the strict standard warning was raised for classes as well. I'm aware of
the
design flaws in the above example; I thought PHP was just behaving
inconsistently
between classes and interfaces. Looks good, then.
Previous Comments:
------------------------------------------------------------------------
[2010-04-13 16:52:49] [email protected]
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
When someone receives an instance of a One object, the method signature
of foo() tells them that it needs an IOne instance. In Two you further
restrict that to an ITwo. Seeing as a Two instance is a One instance, a
Two instance will be accepted by people who want a One instance. They
cannot know that the parameter requirements have been changed and that
is why you get an E_STRICT.
Also, this is not just for interfaces, but for classes as well.
------------------------------------------------------------------------
[2010-04-12 15:43:13] mike at mikegerwitz dot com
Description:
------------
When using an interface for type hinting, PHP raises a strict standards
warning
if an overriding method uses an interface that implements the type hint.
In the
example below, ITwo implements IOne and method One::foo expects the
first
argument to implement IOne. Two extends One and expects the first
argument to
implement ITwo, which implements IOne. This should be allowed, much like
it is
allowed if the interfaces were simply classes.
Test script:
---------------
interface IOne {}
interface ITwo extends IOne {}
class One
{
public function foo( IOne $bla ) {}
}
class Two extends One
{
public function foo( ITwo $bla ) {}
}
class Test implements ITwo {}
// yet, this does work
var_dump( new Test instanceof IOne );
Expected result:
----------------
bool(true)
Actual result:
--------------
PHP Strict Standards: Declaration of Two::foo() should be compatible
with that
of One::foo() in test.php on line 25
Strict Standards: Declaration of Two::foo() should be compatible with
that of
One::foo() in test.php on line 25
bool(true)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51542&edit=1