Hi Joshua,
seams to me the interest in traits decreases rapidly :(
Joshua Thompson schrieb:
Quoted text · 33 lines
trait A1 {
private $a = 'A1';
private function printA() {
echo $this->a;
}
public function callPrintA1() {
$this->printA();
}
}
trait A2 {
private $a = 'A2';
protected function printA() {
echo $this->a;
}
public function callPrintA2() {
$this->printA();
}
}
class A {
include A1 {
public callPrintA1();
}
include A2 {
public printA();
}
public function callPrintA() {
$this->printA();
}
}
$a = new A();
$a->callPrintA1(); // 'A1'
$a->callPrintA2(); // Fatal Error: Undefined method... $a->callPrintA(); // 'A2'
This is what Marcus aso. has been looking for, yes.
Merging Multiple Methods or Properties
Merging is an extension of aliasing. Instead of aliasing properties or methods to unique names, multiple properties or methods are aliased to the same name, and an implementation of the method or a value for the property is placed in the combined class. I do not understand how this is supposed to work with methods? Merging properties is fine, but methods?
Quoted text · 9 lines
Implementation Ideas
- Flatten
- Don't Flatten
Flatten
The idea here is to place all of the properties and methods into the class definition at compile time. This makes it possible to run the code as if traits don't even exist. The benefits of this is that the run-time internals of PHP should not be affected. The downside is that compiling becomes more complicated.
To keep methods and properties from colliding, some kind of alpha-renaming of those that are locally scoped must be made. The most common suggestion is to append the trait name (including namespace) to the front of the method or property seperated by the double colon (::). This should be an acceptable solution to the issue. However, every use of the method or property within the trait, must be changed to this new name.
Don't Flatten
Instead of flattening, the traits could be kept separate from the class. Instead, during runtime, when a call is made to a trait method or property, the correct action is decided. This will require changes to the run-time internals of PHP, but could provide some benefit to opcode caches, as they could cache the trait once and use it for each of the classes that include it.
Well, now traits aren't much distinguishable from classes anymore. Here might be the opportunity to drop it as a separate concept and introduce it as a private mixin concept with the chance to avoid conflicts and the explicit need to make methods/properties non-local/private to the mixin.
But, even if this non-breaking traits/mixins are a nice opportunity to achieve better reuse and avoid conflicts it seams to be more complex. One point I don't like this much, is the need to name every method I do like to have in my class accessible explicitly. This is much more to type it is with my latest proposal.
Hope the other internal folks around keeps on discussing traits or non-breaking traits. I really would like to see one of the discussed flavor making it into the language (well, I'm still revering my own proposal ;) )
Kind Regards
Stefan
Hi
I just want to add a big +1 from an "end user" perspective for traits.
I know from my perspective this would be an incredibly valuable tool for php. I'm essentially already using the functionality this brings (as I'm sure many other devs are), albeit in an ugly way - either adding functionality to a single superclass at the bottom of the class heirarchy, or using interfaces along with composites/delegation, which means a lot of unnecessary code duplication.
I've been reading through the threads on the subject -- I'm not sure if discussion has died because of pressure to get 5.3 out of the door, or for other reasons, but it seems the conversation stopped without any real resolution, and the wiki seems to be in a similar state.
It's a great idea in principle, but I've noticed a lot of discussion is focused on preventing collisions by ignoring/aliasing methods. Maybe I'm misunderstanding the usage case for traits here, but I envision them as "working interfaces". The methods of a trait surely shouldn't change visibiliy/name depending on which class they appear in? Doesn't that defeat the whole point?
Regarding collisions, If I try to do the following...
interface i1{ public function foo(); }
interface i2{ protected function foo($a,$b); }
interface i3{ public function foo($a,$b,$c,$d); }
class Broken implements i1,i2,i3{ private function foo($a) }
.... I wouldn't expect it to work! It's the developer's job to know the interface they're implementing. Similarly isn't it the developer's job to know the methods in a trait, and manage any collisions directly in their code, not in statements for php to process and include/exclude/alias things? Moreover, surely it's the developer's responsibility to use verbose method names (eg 'saveOrder', not 'save'), in the knowledge that the code will be inherited by a class in which his 'save' method would no longer have any context, and is likely to collide with existing methods of that class...
My use scenario for a trait would basically be a functional interface... Since most of the code so far in the discussion on traits has been all foo's and bar's, I'd like to present a few real-life examples that I think show the potential use of traits while still keeping the code compact...
trait Optionable{
protected $_options;
protected function _setOptions($options){
$this->_mergeArrays($this->_options,$options) }
public function getOptions(){ return $this->_options; }
private function _mergeArrays($ar1,$ar2){ }
}
trait Hookable{
private static $registry;
public static function registerHook($type,$object){
self::$registry['...etc...'] } //if methods are copied, I guess it'd have to be Hookable::$registry
protected function _runActionHooks($data){}
protected function _runFilterHooks($data){}
}
class Something is Optionable, Hookable{
//$_options is a protected property of Optionable, so I expect to
inherit or have a copy...
//$registry is private so I don't expect to see it here
public function __construct($data,$options){
//the following methods are all protected, so I'd expect to inherit
or have them copied with the same visibility $this->_setOptions($options);
$this->_runActionHooks($data);
$data = $this->_runFilterHooks($data);
}
}
I know this basically resembles multiple inheritance (please don't cry)... I think the important distinction is firstly one of use - the keyword distinction between class and 'trait' should be used to encourage short, reusable bits of code, where multiple inheritance and the sloppy behaviour that comes with it is not a problem. Secondly, to prevent it becoming multiple-inheritance-by-another-name I don't believe traits should be able to inherit from other traits.
I've read through every message on this subject and I still don't see the benefit being added by all the complexity of adding loads of extra reserved words & complex include/exclude/alias logic for something which should surely be a simple fix to a simple problem?
If I'm being naiive and totally missing something, please let me know!
Andru
+1 from me too please ;)
Indeed the proposed RFC for traits is a wonderfull thing for things we currently do using mixins.
What's goining on with traits anyway? Has it been decided if it's gonna be implemented in 5.3, 6.0, 7.2 or never? :)
G.
On Sat, Jul 26, 2008 at 6:27 AM, Andru Vallance andru@tinymighty.comwrote:
Quoted text · 40 lines
Hi
I just want to add a big +1 from an "end user" perspective for traits.I know from my perspective this would be an incredibly valuable tool for php. I'm essentially already using the functionality this brings (as I'm sure many other devs are), albeit in an ugly way - either adding functionality to a single superclass at the bottom of the class heirarchy, or using interfaces along with composites/delegation, which means a lot of unnecessary code duplication.
I've been reading through the threads on the subject -- I'm not sure if discussion has died because of pressure to get 5.3 out of the door, or for other reasons, but it seems the conversation stopped without any real resolution, and the wiki seems to be in a similar state.
It's a great idea in principle, but I've noticed a lot of discussion is focused on preventing collisions by ignoring/aliasing methods. Maybe I'm misunderstanding the usage case for traits here, but I envision them as "working interfaces". The methods of a trait surely shouldn't change visibiliy/name depending on which class they appear in? Doesn't that defeat the whole point?
Regarding collisions, If I try to do the following...
interface i1{ public function foo(); }
interface i2{ protected function foo($a,$b); }
interface i3{ public function foo($a,$b,$c,$d); }
class Broken implements i1,i2,i3{ private function foo($a) }.... I wouldn't expect it to work! It's the developer's job to know the interface they're implementing. Similarly isn't it the developer's job to know the methods in a trait, and manage any collisions directly in their code, not in statements for php to process and include/exclude/alias things? Moreover, surely it's the developer's responsibility to use verbose method names (eg 'saveOrder', not 'save'), in the knowledge that the code will be inherited by a class in which his 'save' method would no longer have any context, and is likely to collide with existing methods of that class...
My use scenario for a trait would basically be a functional interface... Since most of the code so far in the discussion on traits has been all foo's and bar's, I'd like to present a few real-life examples that I think show the potential use of traits while still keeping the code compact...
trait Optionable{
protected $_options;
protected function _setOptions($options){
$this->_mergeArrays($this->_options,$options) }
public function getOptions(){ return $this->_options; }
private function _mergeArrays($ar1,$ar2){ }
}
trait Hookable{
private static $registry;
public static function registerHook($type,$object){
self::$registry['...etc...'] } //if methods are copied, I guess it'd have to be Hookable::$registry
protected function _runActionHooks($data){}
protected function _runFilterHooks($data){}
}
class Something is Optionable, Hookable{//$_options is a protected property of Optionable, so I expect toinherit or have a copy...
//$registry is private so I don't expect to see it herepublic function __construct($data,$options){ //the following methods are all protected, so I'd expect toinherit or have them copied with the same visibility $this->_setOptions($options);
$this->_runActionHooks($data);
$data = $this->_runFilterHooks($data);
}
}I know this basically resembles multiple inheritance (please don't cry)... I think the important distinction is firstly one of use - the keyword distinction between class and 'trait' should be used to encourage short, reusable bits of code, where multiple inheritance and the sloppy behaviour that comes with it is not a problem. Secondly, to prevent it becoming multiple-inheritance-by-another-name I don't believe traits should be able to inherit from other traits.
I've read through every message on this subject and I still don't see the benefit being added by all the complexity of adding loads of extra reserved words & complex include/exclude/alias logic for something which should surely be a simple fix to a simple problem?
If I'm being naiive and totally missing something, please let me know!
Andru
Quoted text · 5 lines
+1 from me too please ;)
Indeed the proposed RFC for traits is a wonderfull thing for things we currently do using mixins.
What's goining on with traits anyway? Has it been decided if it's
gonna be
implemented in 5.3, 6.0, 7.2 or never? :)
the next PHP version after 5.3.0.
we still need to agree about the syntax of how to deal with
collisions, as this is the key point of this proposal. its not support
to be magical .. its supposed to be explicit.
Lukas Kahwe Smith
mls@pooteeweet.org