Bug #72442 [Opn->Nab]: Return type declarations and inheritance

From: Date: Fri, 17 Jun 2016 17:52:29 +0000
Subject: Bug #72442 [Opn->Nab]: Return type declarations and inheritance
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-201702@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72442&edit=1

 ID:                 72442
 Updated by:         cmb@php.net
 Reported by:        spam at bugyik dot cz
 Summary:            Return type declarations and inheritance
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Ubuntu 16.04
 PHP Version:        7.0.7
-Assigned To:        
+Assigned To:        cmb
 Block user comment: N
 Private report:     N

 New Comment:

> 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>


Previous Comments:
------------------------------------------------------------------------
[2016-06-17 14:56:39] 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



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=72442&edit=1


Thread (3 messages)

« previous php.bugs (#201702) next »