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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: soevilcat at mail dot ru
New email:
PHP Version: OS:

 

 [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: Thu Mar 28 18:01:29 2024 UTC