News Feed
Sections




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

Chris Hartjes' Blog:
Scope Is Not a Mouthwash
November 28, 2011 @ 09:50:03

Chris Hartjes has a reminder posted to his blog today in the form of this recent post that "scope is not a mouthwash" - personal experience from his recent development where he forgot about something as simple as scoping (and it caused him all sorts of headaches).

For [a chapter in my book on dependency injection] I am using Pimple, an incredibly small but effective dependency injection container. Easy to use, simple and effective documentation, just what I was looking for. I also noticed that Pimple supported the use of closures (or anonymous functions) as a way of storing a dependency. Then things got stupid.

He shares a bit of code showing how he added it to his bootstrap but was given a "cannot find class" error when he tried to use the tool. He walks through the steps he followed to track down the problem - looking closer at Pimple, investigating closures and, the ultimate problem, namespace scoping. He was missing a "" to start his namespace and closures work slightly differently:

So why does it behave differently inside closures? I am not 100% sure, but if I had to make an educated guess I would say that when trying to resolve namespaces inside a closure, the interpretor doesn't assume that it is already inside the global namespace, that it is in a namespace of it's own.
0 comments voice your opinion now!
namespace scoping pimple dependencyinjection


blog comments powered by Disqus

Similar Posts

SitePoint PHP Blog: Are PHP Namespaces Really So Bad?

Reddit.com: Attempting to understand Namespacing, and its benefits

Court Ewing's Blog: Zend Framework Modules: Autoloading & Namespaces

Matthew Weier O'Phinney's Blog: Migrating OOP Libraries and Frameworks to PHP 5.3

Padraic Brady's Blog: Namespaces (or Yet Another Pointless Opinion Piece)


Community Events





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


laravel library podcast symfony series introduction artisanfiles version language interview community tool conference framework release voicesoftheelephpant security composer opinion list

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