php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #33347 Function overloading type hinting inconsistencies
Submitted: 2005-06-15 06:45 UTC Modified: 2006-04-03 13:40 UTC
From: levi at alliancesoftware dot com dot au Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.x OS: Fedora Core 2
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: levi at alliancesoftware dot com dot au
New email:
PHP Version: OS:

 

 [2005-06-15 06:45 UTC] levi at alliancesoftware dot com dot au
Description:
------------
When overloading a function and changing the type hinting, php :

- Allows you to change the type hint for a regular overloaded function
- Flags an error when you change the type hint for an implemented abstract function (even for descendants)
- Flags an error when you change the type hint for an implemented interface function (even for descendants)

This happens if the type hinting is at all different: tighter restrictions (subclass of the original type) or looser restrictions (no type hint at all).

Reproduce code:
---------------
<?

class X { }
class Y extends X { }

abstract class Ancestor {
             public function func(X $x) { }
    abstract public function abstractFunc(X $x);
}

interface Inter {
    public function interFunc(X $x);
}

class Descendant extends Ancestor implements Inter {
    public function func(X $x) { }
    public function abstractFunc(X $x) { }
    public function interFunc(X $x) { }
}

class Descendant2 extends Descendant {
    public function func(Y $x) { }          // allowed
    public function abstractFunc(Y $x) { }  // error
    public function interFunc($x) { }       // error
}

?>

Expected result:
----------------
  I would hope the type hints were totally ignored and the parser should only ensure the same number of paramaters.

  At the very minimum, Descendant2 should be allowed to change the type hints even if Descendant was forced to use the original hints. It seems inconsistent to treate ordinary method overloading differently to method overloading from methods that were in an interface or originally abstract.



Actual result:
--------------
PHP Fatal error:  Declaration of Descendant2::abstractFunc() must be compatible with that of Ancestor::abstractFunc() in /home/levi/public_html/testD.php5 on line 23

PHP Fatal error:  Declaration of Descendant2::interFunc() must be compatible with that of Inter::interFunc() in /home/levi/public_html/testD.php5 on line 23

Note that Descendant::abstractFunc() was more restrictive (parameter "Y $y") and Descendant::interFunc() was less restrictive (parameter "$x") -- the error is flagged if the type hint is at all different.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-06-15 15:24 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip


 [2005-06-16 03:32 UTC] levi at alliancesoftware dot com dot au
Occurs with both 5.0 CVS (repository checkout) and 5.1 CVS snapshot (from snapshots page) as of 2005-06-16
 [2006-04-03 13:40 UTC] tony2001@php.net
Implementing an interface means you should follow the rules set by this interface.
And what you're doing is breaking these rules.
I really doub't anybody ever will change PHP do behave this way, as it doesn't make any sense.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 15 13:01:35 2024 UTC