[PHP-BUG] Bug #75385 [NEW]: Fatal error after overriding method and returning subclass of what parent do

[email protected] ("carlos at wfmh dot org dot pl") Sun, 15 Oct 2017 21:41:26 GMT
Newsgroups php.standards
Message-ID <[email protected]>
From:             carlos at wfmh dot org dot pl
Operating system: Ubuntu
PHP version:      7.2.0RC4
Package:          PHP Language Specification
Bug Type:         Bug
Bug description:Fatal error after overriding method and returning subclass of what parent do

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 bug report at https://bugs.php.net/bug.php?id=75385&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=75385&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=75385&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=75385&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=75385&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=75385&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=75385&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=75385&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=75385&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=75385&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=75385&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=75385&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=75385&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=75385&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75385&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=75385&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=75385&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=75385&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75385&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=75385&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=75385&r=mysqlcfg