Edit report at https://bugs.php.net/bug.php?id=75449&edit=1
ID: 75449
Comment by: eric at ericstern dot com
Reported by: alex dot rill at yandex dot ru
Summary: Ignored return type for abstract methods, defined in
traits
Status: Assigned
Type: Bug
Package: Class/Object related
Operating System: Windows
PHP Version: 7.1.11
Assigned To: pmmaga
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2017-11-22 23:28:08] 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.
------------------------------------------------------------------------
[2017-11-21 23:17:46] 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-10-31 02:12:55] 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-10-31 01:43:27] saintatgod at gmail dot com
where is Class D ?
------------------------------------------------------------------------
[2017-10-28 06:25:31] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75449&edit=1