php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #75449 Ignored return type for abstract methods, defined in traits
Submitted: 2017-10-28 06:25 UTC Modified: 2018-11-28 12:42 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: alex dot rill at yandex dot ru Assigned: cmb (profile)
Status: Closed Package: Class/Object related
PHP Version: 7.1.11 OS: Windows
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: alex dot rill at yandex dot ru
New email:
PHP Version: OS:

 

 [2017-10-28 06:25 UTC] alex dot rill at yandex dot ru
Description:
------------
Dont speak english. All in example code.
---
From manual page: http://www.php.net/language.oop5.traits
---


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

trait T {
    abstract static function values(): array;
}

class B {
    use T;
    static function values() {
        return 'Worked!'; // string, not array
    }
}

echo B::values(); // print: Worked!

abstract class B {
    use T;
}

class C extends B {
    static function values() {
        return 'Don\'t worked';
    }
}

echo C::values(); // Fatal Error: Class D contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (C::values)
?>
Online example: http://sandbox.onlinephpfunctions.com/code/7b26e7237abcf97d15aac6a0868c084241bf8d0c


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-10-31 01:43 UTC] saintatgod at gmail dot com
where is Class D ?
 [2017-10-31 02:12 UTC] saintatgod at gmail dot com
<?php
trait T {
        abstract static function values(): array;
}

//class B {
//      use T;
//      static function values() {
//              return 'Worked!'; // string, not array
//      }
//}

//echo B::valuesi(); // print: Worked!

abstract class B {
        use T;
        self::values(); // if inheritance trait, must implement abstract function in trait
}

class C extends B {
        static function values() {
                return 'Don\'t worked';
        }
}

echo C::values();
 [2017-11-21 23:17 UTC] pmmaga@php.net
-Status: Open +Status: Not a bug
 [2017-11-21 23:17 UTC] pmmaga@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is expected behavior. In your first example, the abstract function from the trait is overriden by the function declared on your class. No inheritance rules are applied at all. It's not only about the return value type.

In your second example, you are importing the abstract function to an abstract class. In that case, the inheritance rules are applied when you are extending from that abstract class.
 [2017-11-21 23:18 UTC] pmmaga@php.net
-Assigned To: +Assigned To: pmmaga
 [2017-11-22 23:28 UTC] pmmaga@php.net
-Status: Not a bug +Status: Open
 [2017-11-22 23:28 UTC] pmmaga@php.net
Actually, after re-reading the page on traits, the usage of abstract methods for this purpose is mentioned:

> Traits support the use of abstract methods in order to impose requirements upon the exhibiting class.

Given that, I am re-opening this bug.
 [2018-04-29 21:25 UTC] eric at ericstern dot com
I encountered this as well (discovered this report when filing my own bug, so I'm commenting here rather than filing a dupe). Note that this is in no way specific to static methods.

A similar reproduce case: https://3v4l.org/AVSXN

And more importantly, a test case where the issue that should have been detected at compile-time instead was left until runtime: https://3v4l.org/02d4a This is the main use-case of abstract trait methods.
 [2018-11-28 12:20 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem -Assigned To: pmmaga +Assigned To: cmb
 [2018-11-28 12:20 UTC] cmb@php.net
There has been some discussion about this on the internals@
mailing list[1], and it seem to me that Levi[2] is right.
Therefore I'm changing this to a documentation problem.

[1] <https://externals.io/message/103032>
[2] <https://externals.io/message/103032#103039>
 [2018-11-28 12:41 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=346114
Log: Fix #75449: Ignored return type for abstract methods, defined in traits
 [2018-11-28 12:42 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2018-11-28 12:42 UTC] cmb@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.
 [2020-02-07 06:05 UTC] phpdocbot@php.net
Automatic comment on behalf of cmb
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=7ee0da6a0250d01f4fd96d3c20d36375137b0b06
Log: Fix #75449: Ignored return type for abstract methods, defined in traits
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 03:01:28 2024 UTC