Bug #70270 [NEW]: Interface ignores precedence order of traits

From: Date: Fri, 14 Aug 2015 11:58:23 +0000
Subject: Bug #70270 [NEW]: Interface ignores precedence order of traits
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-195204@lists.php.net to get a copy of this message
From: soevilcat at mail dot ru Operating system: PHP version: 7.0.0beta3 Package: Class/Object related Bug Type: Bug Bug description:Interface ignores precedence order of traits 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) -- Edit bug report at https://bugs.php.net/bug.php?id=70270&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70270&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70270&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70270&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=70270&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=70270&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=70270&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=70270&r=needscript Try newer version: https://bugs.php.net/fix.php?id=70270&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=70270&r=support Expected behavior: https://bugs.php.net/fix.php?id=70270&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=70270&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=70270&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=70270&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70270&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=70270&r=dst IIS Stability: https://bugs.php.net/fix.php?id=70270&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=70270&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=70270&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=70270&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=70270&r=mysqlcfg

« previous php.bugs (#195204) next »