Re: RFC: Traits for PHP - Stateful Traits

From: Date: Fri, 22 Feb 2008 11:20:50 +0000
Subject: Re: RFC: Traits for PHP - Stateful Traits
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-35725@lists.php.net to get a copy of this message
On 21/02/2008, Stefan Marr <php@stefan-marr.de> wrote: > Hi, > > Andi Gutmans schrieb: > > a) > > I think Traits should be able to act as a self-contained behavior which can > > always be expected to work. For example if I want a Counter behavior > I would like > > that not to depend on the properties in the containing class. While I > don't > > think we should enable public nor protected properties in Traits I think > > allowing for private properties in Traits would come in very handy. > It also > > is no problem when it comes to mangling as we can use the Trait name. > > > > class Trait { > > private $counter = 0; > > function getNextSerialNumber() { > > return $this->counter++; > > } > > } > > > > I strongly recommend not to support protected/public and not to even get into > > the discussion of dealing with conflicts of such properties. > > But I think private is very useful. > Hope I got it right, since your example is a class? > > Ok, stateful traits are useful in the sense of having self-contained > units of reuse. Personally, I prefer them over stateless traits. > But, here we will get additional confusion. > > You don't like to handle with visibilities of properties? Fine :) > One way to do stateful traits is described in > > http://www.iam.unibe.ch/~scg/Archive/Papers/Berg07eStatefulTraits.pdf > > But the way you have proposed is stricter and may be sufficient. > > To avoid confusion and misconception I would like to change your > proposal a bit. Private does suggest a semantics like methods, and would > require to apply the flattening on properties like on methods. Since we > do not like to handle conflicts, this would have to be done a bit > different, in my opinion. > > Let's change `private to > local`: > trait TCounter { > local $counter = 0; > function getNextSerialNumber() { > return $this->counter++; > } > } > > class Website { > use TCounter; > } > > The resulting composition would have a property resulting from the > TCounter property, but is only accessible from a method of the specific > trait, i.e., it is composition local. > > Just an additional though :) > > Kind Regards > Stefan I think I see an issue with including properties. If a trait is partially included at different levels of a layered object base uses 2 methods of a trait. extended uses 2 more methods of the same trait another_base uses 1 method of the trait. another_extended_base uses the remaining methods of the trait. You would need to control the inclusion of the properties. -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!"

« previous php.internals (#35725) next »