php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76973 Return type that is alias of base class' type should be compatible
Submitted: 2018-10-04 17:26 UTC Modified: 2019-05-24 16:26 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: ayg at aryeh dot name Assigned:
Status: Duplicate Package: Scripting Engine problem
PHP Version: 7.2.10 OS: Ubuntu
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ayg at aryeh dot name
New email:
PHP Version: OS:

 

 [2018-10-04 17:26 UTC] ayg at aryeh dot name
Description:
------------
If a base class defines a method with a return type declaration, and a derived class overrides the method, the return type in the derived class must be the same as in the base class.  However, if the return types are classes that are aliases of one another, it should be considered the same.  Currently it is not.

Test script:
---------------
<?php
class A {}
class_alias( "A", "B" );
class C { function f() : A { return new A; } }
class D extends C { function f() : B { return new B; } }


Expected result:
----------------
No error

Actual result:
--------------
PHP Fatal error:  Declaration of D::f(): B must be compatible with C::f(): A in Standard input code on line 5


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-10-04 17:59 UTC] nikic@php.net
Comment by spam2 at rhsoft dot net deleted. It was rude, without contributing technical insight wrt this issue.
 [2018-10-04 18:01 UTC] nikic@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: levim
 [2018-10-04 18:01 UTC] nikic@php.net
Long term this is going to be implicitly solved by proper variance support, but for now it might make sense to handle this the same way we do for argument types, i.e. load the classes if they do not pass the string equality checks, but only allow exact matches (no variance).

@levim: What do you think about this?
 [2018-10-04 18:04 UTC] spam2 at rhsoft dot net
rude is such a unreadable sample - code in a style of "} }" is pure effrontery
 [2018-10-04 18:13 UTC] nikic@php.net
-Status: Assigned +Status: Verified -Assigned To: levim +Assigned To:
 [2018-10-04 18:13 UTC] nikic@php.net
Ah, having just checked the implementation we already do what I described. The actual issue is that classes C and D are early-bound (at compile-time), while the class alias is registered later (at run-time). For example, the following code works fine (or the same split across multiple files):

<?php
class A {}
class_alias( "A", "B" );
eval(<<<'CODE'
class C { function f() : A { return new A; } }
class D extends C { function f() : B { return new B; } }
CODE
);

This is a general issue of early-binding and also related to bug #76451.
 [2019-05-24 16:26 UTC] nikic@php.net
-Status: Verified +Status: Duplicate
 [2019-05-24 16:26 UTC] nikic@php.net
As mentioned in my previous comment, this has the same root cause as bug #76451, which is fixed in 7.4. I've verified that the code from this bug report also works in 7.4.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 12:01:27 2024 UTC