On Tue, Nov 16, 2010 at 6:46 PM, Simas Toleikis <sim...@gmail.com> wrote:
> Hello, > > I was just skimming through Traits RFC document on the wiki trying to find > a > practical use for them. The first thing that comes to my mind is a > Singleton > implementation - this is where I found myself literally "copying" most of > the code in the past. As far as I know there is no way to implement this > pattern properly in PHP without duplicate code (due to lack of multiple > inheritance/templating). > > Unfortunately, the same RFC document mentions this for trait methods: > > The static modifier is not supported, because it would change the methods > > semantics and references to $this would break. > > > > .. and that pretty much defeats Singleton-Trait implementation. Is there > any > particular technical/design reasons why traits can't handle $this inside > static methods just like regular static methods do (with a fatal error)? > > p.s. a Singleton-Trait implementation could look like: > > trait Singleton { > public static function getInstance() { ... } > } > > class Child extends Parent { > use Singleton; > } > > Child::getInstance(); > for reusable singleton pattern with php 5.3 http://www.slideshare.net/fabpot/design-patternrevisitedphp53 slide 11 and 12 Tyrael