php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70710 Method compatible declaration issue with parent of class when using trait
Submitted: 2015-10-14 10:12 UTC Modified: 2015-10-15 20:55 UTC
From: wouter at wouter0100 dot nl Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2015-10-14 10:12 UTC] wouter at wouter0100 dot nl
Description:
------------
When you extend a class and use a trait with the same method, you'll get a declaration error. Only by overwriting the extended classes method within the class itself, this is fixed.

Test script:
---------------
Method test: https://3v4l.org/gMEGg
Declaration error: https://3v4l.org/IJoio

Current workaround: https://3v4l.org/gIq0E

Expected result:
----------------
That the (strict) warning isn't being shown and I'm able to execute $this->execute for the parents method and $this->executeTrait for the trait's method.

Actual result:
--------------
See tests.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-15 20:50 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2015-10-15 20:50 UTC] bwoebi@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

PHP is not supporting method overloading.
And it requires declarations to match (Liskov Substitution principle needs to hold: each value I can pass to a parent class must also be allowed to be passed to a child class),

P.s.: In case of a declaration warning, the method is not added at all.
 [2015-10-15 20:55 UTC] bwoebi@php.net
Addendum: note that use ... as ...; instructions in traits are pure aliases. The method with the original name still exists in the class.

See also: https://3v4l.org/ANmkM#v700alpha1 (execute() and executeTrait() are defined)

This is necessary, so that calls between methods inside traits don't break, as traits don't declare their own scope, but are pure compiler-assisted copy&paste.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 13:01:30 2024 UTC