Bug->Doc #75385 [Opn]: PH does not allow covariant return types

From: Date: Mon, 16 Oct 2017 09:22:49 +0000
Subject: Bug->Doc #75385 [Opn]: PH does not allow covariant return types
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-15086@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 Updated by: cmb@php.net Reported by: carlos at wfmh dot org dot pl -Summary: Fatal error after overriding method and returning subclass of what parent do +Summary: PH does not allow covariant return types Status: Open -Type: Bug +Type: Documentation Problem -Package: *General Issues +Package: Scripting Engine problem Operating System: Ubuntu PHP Version: 7.2.0RC4 Block user comment: N Private report: N New Comment: It seems that is not properly documented. Previous Comments: ------------------------------------------------------------------------ [2017-10-15 21:48:35] Wes dot example at example dot org Sadly php doesn't support return type covariance yet. For now you can doc-comment /** @return ModelChild */ in class B. ------------------------------------------------------------------------ [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.doc.bugs (#15086) next »