Doc #75385 [Opn->Csd]: PHP does not support covariant return types

From: Date: Sun, 16 Feb 2020 17:39:34 +0000
Subject: Doc #75385 [Opn->Csd]: PHP does not support covariant return types
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-17313@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: carusogabriel@php.net Reported by: carlos at wfmh dot org dot pl Summary: PHP does not support covariant return types -Status: Open +Status: Closed Type: Documentation Problem Package: Scripting Engine problem Operating System: Ubuntu PHP Version: 7.2.0RC4 -Assigned To: +Assigned To: carusogabriel Block user comment: N Private report: N New Comment: The support for Covariant Return and Parameters Types was added in PHP 7.4 via https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters. Previous Comments: ------------------------------------------------------------------------ [2017-10-16 09:22:48] cmb@php.net It seems that is not properly documented. ------------------------------------------------------------------------ [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 (#17313) next »