php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74315 Fatal error: Declaration of test2::test() must be compatible with test1::test()
Submitted: 2017-03-26 07:02 UTC Modified: 2017-03-27 17:42 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: spam2 at rhsoft dot net Assigned: cmb (profile)
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.1.3 OS: Linux
Private report: No CVE-ID: None
 [2017-03-26 07:02 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-27 17:42 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2017-03-27 17:42 UTC] cmb@php.net
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-27 17:47 UTC] spam2 at rhsoft dot net
"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?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 10:01:32 2024 UTC