|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2016-03-27 06:28 UTC] krakjoe@php.net
-Status: Open
+Status: Closed
-Assigned To:
+Assigned To: krakjoe
[2016-03-27 06:28 UTC] krakjoe@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 07:00:01 2025 UTC |
Description: ------------ According to docs, trait's methods take precedence over inherited methods. However, interfaces seem to ignore trait's methods if the class has a parent... The script below has some marked parts: A - extending from a parent class; B - applying a trait; C - local function member. All three points supply a method named "get_value". The order of precedence between them is C, B, A: local member; if absent, then trait's member; if absent, then parent's member. The Interface requires get_value() method with a specific signature provided by either part B or C. The parent class has a different signature, though only by optional arguments. The script compiles with parts A, B and C left in. However, if you delete part C (current class members; that means that trait's member does take precedence, and you can easily confirm that instantiating an object and calling the method), it gives off fatal error: Fatal error: Declaration of Value::get_value() must be compatible with ValueHost::get_value($code = NULL) If you delete parts C and A, leaving only trait's member (no parent class), then there's no fatal error. The interface takes precedence of parent's method over trait's method while the composition doesn't! In the script below, part C is commented for demonstration purposes. Test script: --------------- interface ValueHost { public function get_value($code=null); } trait Value_has_registers { public function get_value($code=null) { if ($code===null) return parent::get_value(); else return $this->regs[$code]; } } abstract class Value { protected $content; public function get_value() { return $this->content; } } class Value_entity /* (A) */ extends Value /* (A) ends */ implements ValueHost { /* (B) */ use Value_has_registers; /* (B) ends */ /* (C) public function get_value($code=null) { if ($code===null) return parent::get_value(); else return $this->regs[$code]; } (C) ends */ } Expected result: ---------------- no errors Actual result: -------------- Fatal error: Declaration of Value::get_value() must be compatible with ValueHost::get_value($code = NULL)