php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #55183 Aliasing traits
Submitted: 2011-07-11 16:50 UTC Modified: 2011-07-11 18:38 UTC
From: php at bouchery dot com Assigned: gron (profile)
Status: Closed Package: Class/Object related
PHP Version: 5.4.0alpha1 OS: Windows XP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php at bouchery dot com
New email:
PHP Version: OS:

 

 [2011-07-11 16:50 UTC] php at bouchery dot com
Description:
------------
I have 2 traits with the same method name. I don't want to keep one and rename 
(alias) the other, but rename both.


	use X, Y {
		X::set as setX;
		Y::set as setY;
	}

Test script:
---------------
<?php
trait X {
	protected $_x = 0;
	public function set($x)	{
		$this->_x = $x;
	}
}

trait Y {
	protected $_y = 0;
	public function set($y)	{
		$this->_y = $y;
	}
}

class MyClass {
	use X, Y {
		X::set as setX;
		Y::set as setY;
	}
}
?>

Expected result:
----------------
MyClass with 2 method and 2 properties : 
class MyClass
{
	protected $_x = 0;
	protected $_y = 0;
	public function setX($x) {
		$this->_x = $x;
	}

	public function setY($y) {
		$this->_y = $y;
	}
}


Actual result:
--------------
PHP Fatal error:  Trait method set has not been applied, because there are 
collisions with other trait methods on MyClass

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-11 18:15 UTC] felipe@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: gron
 [2011-07-11 18:38 UTC] gron@php.net
-Status: Assigned +Status: Closed -Type: Bug +Type: Feature/Change Request
 [2011-07-11 18:38 UTC] gron@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

Hi:

Apologies for marking it "bogus", the bug-tracker isn't really polity.
Your use case is perfectly valid, but not supported.

The current behavior was designed this way intentionally.
We had lengthy discussions about an explicit exclude operator on the mailing 
list, and the consensus was that it is not desirable to have it, 
because it would break the consistency of a trait. (I did not follow that 
argument, since traits are not strongly consistent in anyway, but that was the 
result of the discussions.)

Thus, I am sorry, but this is not possible.

Furthermore, aliasing is NOT renaming.
As you noticed, it does not change anything, but just provides an _additional_ 
alias to the same method body.

I will close this issue, but feel free to start a discussion on the mailing list 
if you feel this is of greater importance.

Thanks
Stefan
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 03:01:28 2024 UTC