php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51542 Overriding method type hint with child interface raises strict standards
Submitted: 2010-04-12 15:43 UTC Modified: 2010-04-13 16:52 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: mike at mikegerwitz dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.2 OS: GNU/Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mike at mikegerwitz dot com
New email:
PHP Version: OS:

 

 [2010-04-12 15:43 UTC] 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)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-13 16:52 UTC] degeberg@php.net
-Status: Open +Status: Bogus
 [2010-04-13 16:52 UTC] degeberg@php.net
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-13 19:47 UTC] mike at mikegerwitz dot com
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 17:01:33 2024 UTC