|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 15:00:01 2025 UTC |
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.