php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #78325 Function overloading type prevents overloading
Submitted: 2019-07-23 08:26 UTC Modified: 2019-07-23 08:33 UTC
From: dallas at ekkysoftware dot com Assigned: cmb (profile)
Status: Not a bug Package: Class/Object related
PHP Version: 7.1.30 OS: Both
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dallas at ekkysoftware dot com
New email:
PHP Version: OS:

 

 [2019-07-23 08:26 UTC] dallas at ekkysoftware dot com
Description:
------------
Issues with overriding parameters of the same function with different types is causing a compilation errors that I don't know how to fix.

If there is a fix, can you please let me know and put it somewhere on the Internet so Google and I can find it.

Test script:
---------------
class Fie{
protected $value;

function copy(Fie $new) : Fie
{
$this->value = $new->value;
return $this;
}
}

class Foh extends Fie{
protected $field;

function copy(Foh $new) : Foh
{
$this->field = $new->field;
return parent::copy($new);
}
}

Expected result:
----------------
Not needing to totally rewrite Fie's code in Foh.


$n = new Foh();
$b = new Foh();
$b->copy($n);

Actual result:
--------------
PHP Fatal error:  Declaration of Foh::copy(Foh $new): Foh must be compatible with Fie::copy(Fie $new): Fie in test_file.php on line 29

Fatal error: Declaration of Foh::copy(Foh $new): Foh must be compatible with Fie
::copy(Fie $new): Fie in test_file.php on line 29


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-07-23 08:31 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2019-07-23 08:31 UTC] cmb@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.

You're trying to use covariant method signatures here, which
violates the Liskov Substitution Principle, and is therefore not
allowed in PHP.
 [2019-07-23 08:33 UTC] nikic@php.net
The return type change is allowed in PHP 7.4 (the parameter type change is not, as it is unsound).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Nov 13 06:01:32 2024 UTC