News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Benjamin Eberlei:
Traits are Static Access
April 12, 2013 @ 11:16:35

In a new post to his site Benjamin Eberlei shares an opinion about traits, noting that they're basically the same as static functionality when it comes to several things like coupling, not being testable and being a "global state" container.

I used to look forward to traits as a feature in PHP 5.4, but after discussions with Kore I came to the conclusion that traits are nothing else than static access in disguise. They actually lead to the exact same code smells. Familiar with the outcome of too much static use, we should reject traits as just another way of statically coupling your code to other classes.

He includes some code examples showing traits in use in an example controller to handle a simple redirect. He points out at least six different issues with just this simple implementation. He rewrites it as "static" code to help prove his point. He comes to the conclusion that, much like static methods, traits should be avoided and instead aggregation should be favored.

0 comments voice your opinion now!
traits static compare avoid example code

Link: http://www.whitewashing.de/2013/04/12/traits_are_static_access.html

blog comments powered by Disqus

Similar Posts

Chris Roane's Blog: The Last Few Years: What a PHP Programmer Has Learned

Daniel Cousineau's Blog: Serialize Python Variables To PHP

Richard Smaizys' Blog: Improve your code style with simple tips

Jani Hartikainen's Blog: Sending files better: Apache mod_xsendfile and PHP

KillerPHP Blog: PHP Interfaces: when and why you should use them instead of classes?


Community Events

Don't see your event here?
Let us know!


part2 application yii2 community opinion series interview programming introduction php7 example podcast list symfony laravel project composer api language framework

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework