Bug #75385 [Com]: Fatal error after overriding method and returning subclass of what parent do

From: Date: Sun, 15 Oct 2017 21:48:37 +0000
Subject: Bug #75385 [Com]: Fatal error after overriding method and returning subclass of what parent do
References: 1  Groups: php.standards 
Request: Send a blank email to standards-+get-1299@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75385&edit=1 ID: 75385 Comment by: Wes dot example at example dot org Reported by: carlos at wfmh dot org dot pl Summary: Fatal error after overriding method and returning subclass of what parent do Status: Open Type: Bug Package: PHP Language Specification Operating System: Ubuntu PHP Version: 7.2.0RC4 Block user comment: N Private report: N New Comment: Sadly php doesn't support return type covariance yet. For now you can doc-comment /** @return ModelChild */ in class B. Previous Comments: ------------------------------------------------------------------------ [2017-10-15 21:41:24] carlos at wfmh dot org dot pl Description: ------------ Having script like that <?php class ModelBase {} class ModelChild extends ModelBase {} class A { public function test(): ModelBase {} } class B extends A { public function test(): ModelChild {} } ordinary php file.php throws PHP Fatal error: Declaration of B::test(): ModelChild must be compatible with A::test(): ModelBase in file.php on line 13 PHP 7.2.0RC3 (cli) (built: Sep 28 2017 16:28:37) ( NTS ) Copyright (c) 1997-2017 The PHP Group Test script: --------------- <?php class ModelBase {} class ModelChild extends ModelBase {} class A { public function test(): ModelBase {} } class B extends A { public function test(): ModelChild {} } Expected result: ---------------- As ModelChild extends ModelBase, methods expected to return ModelBase should be allowed to be overriden and return its subclass (ModelChild) as it is allowed in any other language that follows OOP principles. Actual result: -------------- PHP Fatal error: Declaration of B::test(): ModelChild must be compatible with A::test(): ModelBase in file.php on line 13 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75385&edit=1

« previous php.standards (#1299) next »