php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71414 Fatal error when using interface trait and inheritance
Submitted: 2016-01-19 19:16 UTC Modified: 2016-01-19 22:41 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: krzysiekpiasecki at gmail dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 7.0.2 OS: OS X 10.9.5
Private report: No CVE-ID:
 [2016-01-19 19:16 UTC] krzysiekpiasecki at gmail dot com
Description:
------------
Interface method override inherited method and implemented in a trait causes fatal error

Test script:
---------------
interface InterfaceY {
    public function y(): string;
}

trait TraitY {
    public function y(): string {
    }
}

class X {
    public function y() {
    }
}

class Y extends X implements InterfaceY {
    use TraitY;
}


Expected result:
----------------
No fatal error. Method Y in TraitY is well defined

Actual result:
--------------
Fatal error:  Declaration of X::y() must be compatible with Y::y(): string


PS


__No fatal error__ when:


class Y extends X implements InterfaceY {
    use TraitY;
    public function y(): string {
    }
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-01-19 20:16 UTC] cmb@php.net
-Package: PHP Language Specification +Package: Class/Object related
 [2016-01-19 21:18 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2016-01-19 21:18 UTC] requinix@php.net
It is not "well defined": X's y() must also have the string return type declaration to be compatible.
https://wiki.php.net/rfc/return_types

https://3v4l.org/lG7t4
 [2016-01-19 22:24 UTC] krzysiekpiasecki at gmail dot com
I wrote, that method Y in TraitY is well defined and it is according to InterfaceY


No fatal error when method public y():string is defined in the class body not only in a trait. What's the difference?

    class Y extends X implements InterfaceY {
        use TraitY;
        public function y(): string {

        }
    }
 [2016-01-19 22:41 UTC] requinix@php.net
-Status: Not a bug +Status: Open
 [2016-01-19 22:41 UTC] requinix@php.net
Oh, I see what you mean. The trait's method should overwrite the inherited method, and since stuff like https://3v4l.org/dCZ9h works without the trait, using the trait should also be allowed.
 [2016-03-26 23:55 UTC] krakjoe@php.net
Automatic comment on behalf of krakjoe
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4327370d1308eab3c611fc6994e67048a18f92ef
Log: fix #71414 (Interface method override inherited method and implemented in a trait causes fatal error)
 [2016-03-26 23:55 UTC] krakjoe@php.net
-Status: Open +Status: Closed
 [2016-03-26 23:58 UTC] bwoebi@php.net
Automatic comment on behalf of krakjoe
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4327370d1308eab3c611fc6994e67048a18f92ef
Log: fix #71414 (Interface method override inherited method and implemented in a trait causes fatal error)
 [2016-07-20 11:32 UTC] davey@php.net
Automatic comment on behalf of krakjoe
Revision: http://git.php.net/?p=php-src.git;a=commit;h=4327370d1308eab3c611fc6994e67048a18f92ef
Log: fix #71414 (Interface method override inherited method and implemented in a trait causes fatal error)
 [2016-12-16 12:27 UTC] lisachenko dot it at gmail dot com
Looks like this fix introduced behaviour changes for prototype methods, see https://3v4l.org/LLebD
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC