php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #65303 Make traits more flexibile
Submitted: 2013-07-21 00:12 UTC Modified: 2013-07-23 10:16 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: thomas dot ene at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.4.17 OS: -
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: thomas dot ene at gmail dot com
New email:
PHP Version: OS:

 

 [2013-07-21 00:12 UTC] thomas dot ene at gmail dot com
Description:
------------
I find traits very useful (i've been waiting for their availability for a long time) - however I think people could benefit from having a bit more flexibility when using them - specifically when dealing with trait method renaming - rather then being forced to choose one method over the other, I think it would be useful to have the option to rename all methods from all traits to our liking. I've posted an example in the test/script section.



Test script:
---------------
trait dog {
	public function bark() {
		echo("Bark, bark, bark!<br>");
		$this->breathe();
	}
	public function breathe() {
		echo("...dog breathing...<br>");
	}
}

trait snake {
	public function hiss() {
		echo("ssssss!<br>");
		$this->breathe();
	}
	public function breathe() {
		echo("...snake breathing...<br>");
	}
}

class pugosnake {
	use dog, snake {
		//What I think it would be useful is this
		dog::breathe as dogBreathe;
		dog::bark as dogBark; // which will call the correct breathe function (dogBreathe now);
		snake::breathe as snakeBreathe;
		snake::hiss as snakeHiss; // which will also call the correct breathe function (snakeBreathe now);
	}
}

$a = new pugosnake();
$a->dogBreathe(); // Produces "Bark, bark, bark...dog breathing..."
$a->snakeBreathe(); // Produces "ssssss...snake breathing..."

// Right now this doesn't work as we're forced to select one breathe method over the other



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-22 08:50 UTC] gron@php.net
-Status: Open +Status: Not a bug
 [2013-07-22 08:50 UTC] gron@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Hi Thomas:

If I understand you correctly, it already works what you are asking for.

So, this should work:

dog::breathe as dogBreathe;
dog::bark as dogBark;
snake::breathe as snakeBreathe;
snake::hiss as snakeHiss;

See the last part of Example #5: 
http://php.net/manual/en/language.oop5.traits.php 
However, since `as` introduces an alias and does not do renaming, you still need 
to resolve the conflicts explicitly with 
`insteadof`.

This is on purpose. While you might argue that it is not 'DRY', it makes you 
formulate explicitly what you need, and therefore 
provides some protection from unexpected changes in the traits hierarchy.

Hope this helps
Stefan
 [2013-07-23 10:16 UTC] thomas dot ene at gmail dot com
Many thanks Stefan, with your last comment it makes sense. Apologies for submitting this as a bug though, I was actually looking for a "feature request" section - and the closest I could find was: "Bug"/"Feature/change request".

Best Regards,
Thomas
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 05:01:33 2025 UTC