Req #78325 [Nab]: Function overloading type prevents overloading
| From: | nikic@php.net | Date: | Tue, 23 Jul 2019 08:33:36 +0000 |
| Subject: | Req #78325 [Nab]: Function overloading type prevents overloading | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-221905@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78325&edit=1
ID: 78325
Updated by: nikic@php.net
Reported by: dallas at ekkysoftware dot com
Summary: Function overloading type prevents overloading
Status: Not a bug
Type: Feature/Change Request
Package: Class/Object related
Operating System: Both
PHP Version: 7.1.30
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
The return type change is allowed in PHP 7.4 (the parameter type change is not, as it is unsound).
Previous Comments:
------------------------------------------------------------------------
[2019-07-23 08:31:10] 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:26:55] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=78325&edit=1