Bug #74315 [Com]: Fatal error: Declaration of test2::test() must be compatible with test1::test()
[email protected] ("spam2 at rhsoft dot net")
| Newsgroups | php.bugs |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=74315&edit=1
ID: 74315
Comment by: spam2 at rhsoft dot net
Reported by: spam2 at rhsoft dot net
Summary: Fatal error: Declaration of test2::test() must be
compatible with test1::test()
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Linux
PHP Version: 7.1.3
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
"If test2::test() returns anything else than an array" - yeah - IF
but how do you imagine introduce return-types even when you have control over all code wich extends classes with this chicken-egg-problem that you can't add the return-types on the underlying class and after deploy the libraries fix based on warnings code extending them when all that sites are actually DOWN?
Previous Comments:
------------------------------------------------------------------------
[2017-03-27 17:42:02] [email protected]
If test2::test() returns anything else than an array, it would be
a violation of the LSP. It's better to catch that potential
pitfall early, and so a fatal error is raised. This is clearly not
a bug, and I don't think that a respective RFC would pass, but
feel free to submit one[1]. :)
[1] <https://wiki.php.net/rfc/howto>
------------------------------------------------------------------------
[2017-03-26 07:02:25] spam2 at rhsoft dot net
Description:
------------
it's annoying enough at all that you get warnings when method declarations are not 100% identical in case of adding param-type-hints in the underlying classes without change all extended classes at the same time but in case of return types a fatal error is just unacceptable
(and no it also don't make sense that you can't skip optional params in a extended class or add additional ones to change the behavior of the parent-call)
________________________________
unacceptable because you can't add them at all without hard breaking every dervied code while in case of the annoying warnings you can at least adopt the changes while watching errorlog and sites ar enot completly broken
[harry@srv-rhsoft:/downloads]$ php test.php
Fatal error: Declaration of test2::test() must be compatible with test1::test(): array in /mnt/data/downloads/test.php on line 2
Test script:
---------------
[harry@srv-rhsoft:/downloads]$ cat test.php
<?php
class test2 extends test1
{
public function test()
{
}
}
class test1
{
public function test(): array
{
}
}
?>
Expected result:
----------------
no error and in the best case even no warning at all, extedning a class and override methods in doubt always imply changed behavior with or without changing method signatures
Actual result:
--------------
Fatal error: Declaration of test2::test() must be compatible with test1::test(): array in /mnt/data/downloads/test.php on line 2
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74315&edit=1