Bug #73985 [Opn->Dup]: Return type of the child may not match the parent

From: Date: Tue, 24 Jan 2017 14:29:37 +0000
Subject: Bug #73985 [Opn->Dup]: Return type of the child may not match the parent
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-206921@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=73985&edit=1 ID: 73985 Updated by: requinix@php.net Reported by: david at grudl dot com Summary: Return type of the child may not match the parent -Status: Open +Status: Duplicate Type: Bug Package: Class/Object related PHP Version: 7.1.1 Block user comment: N Private report: N New Comment: This happens because the method is being validated against the original definition, not the parent. Since test() was defined in I, A::test is validated against I::test (succesfully) and B::test is validated against I::test (successfully) so the A<->B problem won't be reported. You can see this behavior by making I::example() return int: https://3v4l.org/V71FR > Fatal error: Declaration of B::test(): string must be compatible with I::test(): int I just wrote up a longer bug report about this behavior (which also happens with method parameters) as bug #73987 so I'm marking this one as a duplicate. Previous Comments: ------------------------------------------------------------------------ [2017-01-24 12:54:47] david at grudl dot com Description: ------------ Return type of the child must match the parent: This code leads to "Fatal error: Declaration of B::test(): string must be compatible with A::test(): int" <?php class A { function test(): int {} } class B extends A { function test(): string {} } ?> But it's okay, if I add interface: <?php interface I { function test(); } class A implements I { function test(): int {} } class B extends A { function test(): string {} } ?> Expected result: ---------------- Fatal error: Declaration of B::test(): string must be compatible with A::test(): int Actual result: -------------- no error ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=73985&edit=1

« previous php.bugs (#206921) next »