News Feed
Sections




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

Rob Allen:
Use statements
March 17, 2014 @ 10:13:08

Rob Allen's latest post focuses in on something that's been a part of PHP for a while now, back when namespacing was introduced - the "use" keyword. He shares some thoughts, both from others and himself, about whether or not they make code more readable.

I was having a discussion on IRC about use statements and whether they improved code readability or not. [...] Those longer class names make it a little hard to quickly parse what it going on. The [example with "use" statements] is clearly less cluttered, but is at the expense of ambiguity. Exactly what class is User? I would have to go to the top of the file to find out. Should I use aliases? If so, how should I name them?

He went out to Twitter for advice from other PHP developers on the issue too. The feedback from his question came mostly in support of the "use" statements:

  • "I think use statements just abstract where the class is coming from. Some people find that useful."
  • "I think it's helpful seeing all of the packages used by a class without having to look through the full code."
  • "One reason I like them is that I can glance at a file and know dependencies immediately."
  • "I do appreciate what you are saying about the indirection use statements introduce."

There's also a bit of talk about "aliasing" with namespaces rather than the full classname, then using the namespace and class name in the code to "minimise ambiguity".

0 comments voice your opinion now!
use statement namespace twitter advice feedback alias

Link: http://akrabat.com/php/use-statements/

blog comments powered by Disqus

Similar Posts

Jeremy Privett's Blog: Is PHP Doomed?

Ralph Schindler's Blog: Autoloading (Revisited)

Ben Ramsey's Blog: Lampooning Benchmarks

Josh Adell: Interfaces and Traits: A Powerful Combo

SitePoint Web Blog: From Developer to Product Manager: A 3 Stage Plan


Community Events





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


application laravel wordpress library bugfix release interview developer list code project api configure framework threedevsandamaybe language podcast community series introduction

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