Traits and static methods

From: Date: Tue, 16 Nov 2010 17:46:09 +0000
Subject: Traits and static methods
Groups: php.internals 
Request: Send a blank email to internals+get-50258@lists.php.net to get a copy of this message
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();

« previous php.internals (#50258) next »