Doc #77294 [Opn]: Parent methods can "fulfill requirements" of child trait abstract methods
| From: | cmb@php.net | Date: | Thu, 13 Dec 2018 18:12:08 +0000 |
| Subject: | Doc #77294 [Opn]: Parent methods can "fulfill requirements" of child trait abstract methods | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-16214@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77294&edit=1
ID: 77294
Updated by: cmb@php.net
Reported by: chasepeeler at gmail dot com
Summary: Parent methods can "fulfill requirements" of child
trait abstract methods
Status: Open
Type: Documentation Problem
Package: Class/Object related
Operating System: Windows
PHP Version: 7.0.33
Block user comment: N
Private report: N
New Comment:
> And while someone's in there they can fix the "fullfill" spelling.
Oops, my bad. Fixed[1].
> Calling traits "copy/paste" is a simplification. They're more
> like mixins.
The RFC[2], however, says:
| As already mentioned, multiple inheritance and Mixins are
| complex mechanisms. Traits are an alternative which have been
| designed to impose no additional semantics on classes. [â¦] It is
| almost like a language supported and failsafe copy'n'paste
| mechanism to build classes.
> Surely this is more useful than the compiler raising an error, right?
However, the compiler raises an error:
Fatal error: Declaration of parentImplementedTH::bar(array $a) must be compatible with
abstractBarTH::bar(int $a) in /in/OKoTY on line 57
This contradicts the statement:
| A concrete class fulfills this requirement by defining a
| concrete method with the same name; its signature may be
| different.
which was supposed to fix bug #75449.
[1] <http://svn.php.net/viewvc?view=revision&revision=346323>
[2] <https://wiki.php.net/rfc/horizontalreuse>
Previous Comments:
------------------------------------------------------------------------
[2018-12-13 17:22:22] requinix@php.net
Calling traits "copy/paste" is a simplification. They're more like mixins.
As my edit to the summary suggests, what's happening is that the parent class is defining a
method (via a trait, which is unimportant), the child's trait is defining a *requirement* of a
method, and the parent is meeting the requirement. Surely this is more useful than the compiler
raising an error, right?
The "Abstract Trait Members" section already addresses this idea but could gain a sentence
saying that the fulfilling can be done by a parent. And while someone's in there they can fix
the "fullfill" spelling.
------------------------------------------------------------------------
[2018-12-13 16:04:22] chasepeeler at gmail dot com
Description:
------------
If a child class uses a trait with an abstract method, while it's parent class uses a trait
with a defined method of the same name, the method from the parent classes trait will act as the
implementation of the abstract method.
The issue also exists in cases where a class uses two traits. One trait contains an abstract method
while the other trait contains a defined method of the same name. When executed, the defined method
will act as the implementation of the abstract method.
I don't know if this is a bug or an undocumented feature. If it isn't a bug, then it
breaks the "compiler assisted copy/paste rule" since copy/pasting the traits into the
classes would cause an error. It also appears to violate the documented behavior in relation to
conflict resolution: http://php.net/manual/en/language.oop5.traits.php#language.oop5.traits.conflict.
If it isn't a bug, then the documentation needs to be updated to better explain this situation.
Test script:
---------------
https://3v4l.org/OKoTY
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77294&edit=1