php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70270 Interface ignores precedence order of traits
Submitted: 2015-08-14 11:57 UTC Modified: 2016-03-27 06:28 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: soevilcat at mail dot ru Assigned: krakjoe (profile)
Status: Closed Package: Class/Object related
PHP Version: 7.0.0beta3 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
42 - 12 = ?
Subscribe to this entry?

 
 [2015-08-14 11:57 UTC] soevilcat at mail dot ru
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)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [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
This was fixed with #71414, no longer reproducible.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Apr 21 13:01:27 2024 UTC