php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #29417 Type hints do not allow for extended interfaces
Submitted: 2004-07-28 03:41 UTC Modified: 2006-01-13 09:27 UTC
Votes:7
Avg. Score:4.7 ± 0.7
Reproduced:6 of 6 (100.0%)
Same Version:0 (0.0%)
Same OS:3 (50.0%)
From: ah08010-zend at yahoo dot com Assigned: helly (profile)
Status: Wont fix Package: Feature/Change Request
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2004-07-28 03:41 UTC] ah08010-zend at yahoo dot com
Description:
------------
Type hints in a derived class 
    that specify a function parameter 
        implements an interface 
            derived from the interface 
        specified in the (other) interface
implemented by the base class 
(whew!) 
are flagged as an error despite being type-compatible:

Fatal error: Declaration of TranslatedPlural::compose() must be compatible with that of Translatable::compose() in ...\TranslatedMessage.php on line 83


Reproduce code:
---------------
// Parameter type hint (BASE)
interface TranslationMap
{
	public	function fetch($the_key);
}

// Parameter type hint (DERIVED)
interface TranslationLexicon
	extends TranslationMap
{
	public function plural($the_noun, $the_count);
}

// Implementation Interface
interface Translatable
{
	function set_args();
	function compose(TranslationMap $map);
}

// Base class
class TranslatedMessage
	implements Translatable
{
	public function set_args()
	{
	}	

	public function compose(TranslationMap $lex)
	{
        }
}

// Derived class
class TranslatedPlural
	extends TranslatedMessage
{
    public function compose(TranslationLexicon $lex)
    {
    }
}


Expected result:
----------------
I expect that the derived class, TranslatedPlural, will be checked for a stricter (but type-compatible) interface than the base class.

Actual result:
--------------
Fatal error: Declaration of TranslatedPlural::compose() must be compatible with that of Translatable::compose() in ...\TranslatedMessage.php on line 83

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-07-28 05:58 UTC] helly@php.net
Wow it seems you'd be the first person answering my OOP test question correctly. Anyway structer is the wrong word here but you are right. The code you have here should work. If you wanna experiment with that you can try this patch: http://marcus-boerger.de/php/ext/ze2/ze2-type-hint-classes-20040719.diff.txt

But though it works in CLI this is not even a proof of concept.
 [2006-01-13 02:02 UTC] matt at builtfromsource dot com
Well, I just ran into this problem as well.  Looks like it's been around for awhile.  Any fix on the horizon?
 [2006-01-13 09:27 UTC] helly@php.net
Sorry, we decided to not fix it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 08:01:35 2024 UTC