php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #72442 Return type declarations and inheritance
Submitted: 2016-06-17 14:56 UTC Modified: 2017-08-16 16:29 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: spam at bugyik dot cz Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: 7.0.7 OS: Ubuntu 16.04
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: spam at bugyik dot cz
New email:
PHP Version: OS:

 

 [2016-06-17 14:56 UTC] spam at bugyik dot cz
Description:
------------
There is not possible to define return type parent object in abstract method,

Online demo: https://3v4l.org/innXW

From documentation:
The same types are available for return type declarations as are available for argument type declarations[1]

The parameter must be an ==INSTANCEOF== the given class or interface name[2]

[1] http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration

[2] http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.types

Test script:
---------------
<?php

abstract class Entity {}
class FooEntity extends Entity {}

abstract class Repository
{
    abstract public function getDetail(): Entity;
}

class FooRepository extends Repository
{
    public function getDetail(): FooEntity
    {
        return new FooEntity;
    }
    
}

$obj = new FooRepository();
$obj->getDetail();

Expected result:
----------------
No error expected.

Actual result:
--------------
Fatal error: Declaration of FooRepository::getDetail(): FooEntity must be compatible with Repository::getDetail(): Entity in /in/innXW on line 18


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-17 17:52 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-06-17 17:52 UTC] cmb@php.net
> The parameter must be an ==INSTANCEOF== the given class or
> interface name[2]

This section is about the types of the actual parameters (aka.
arguments), but not about formal parameters. The documentation
might be a bit sloppy there; feel free to file a separate
documentation bug report.

Anyhow, the behavior you're reporting as bug is prominently
documented[1]:

> When overriding a parent method, the child's method must match
> any return type declaration on the parent.

Currently, PHP implements invariant parameter and return types
only, i.e. no covariance or contravariance. There is already
request #71825 to add this feature.

[1] <http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration>
 [2016-09-23 18:08 UTC] cmb@php.net
-Status: Not a bug +Status: Suspended
 [2016-09-23 18:08 UTC] cmb@php.net
Well, <https://bugs.php.net/71825> is about parameters, but this
is about return types, so I'm re-opening, changing to feature
request and suspend the ticket, because it requires an RFC, see
<https://wiki.php.net/rfc/howto>.
 [2016-09-23 18:08 UTC] cmb@php.net
-Type: Bug +Type: Feature/Change Request
 [2017-08-16 16:29 UTC] cmb@php.net
-Assigned To: cmb +Assigned To:
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 00:01:32 2024 UTC