I love PHP, I really do… just, sometimes you fall into these situations where you just can’t help bang your head against the table until even the table starts bleeding.
I’m currently working on a blog series about intelligent caching with PHP. During the preparation for this I had to create a framework which could demonstrate the caching concepts I was going to be discussing, and what better than using namespaces for it. Namespaces makes auto loading easy as pie, they give you an incredible freedom in class naming due to the extra encapsulation layer and in general they’re just brilliant. Except for this.
$controller = '\Evil\Controller\News'; $data = $controller::dataKeyInvalidates($invalidate);
It simply wouldn’t work. I was certain it worked fine without namespaces so for kicks I tried this:
$data = \Evil\Controller\News::dataKeyInvalidates($invalidate);
And it ran without problems. Fun. What eventually let me figure out what was going on was by echoing the variable passed to my auto loader. When I used a variable to reference the class name the auto loader was passed “\Evil\Controller\News” when I didn’t the auto loader was passed “Evil\Controller\News”. Obviously some magic goes on inside PHP that translates \Evil\Controller\News into “Evil\Controller\News”. Setting $controller to ‘Evil\Controller\News’ made it work perfectly fine.
So to recap this:
// Does not work. $controller = '\Evil\Controller\News'; $data = $controller::dataKeyInvalidates($invalidate); // Works. $controller = 'Evil\Controller\News'; $data = $controller::dataKeyInvalidates($invalidate); // Works. $data = \Evil\Controller\News::dataKeyInvalidates($invalidate); // Does not work. $data = Evil\Controller\News::dataKeyInvalidates($invalidate);
- Previous Post
- Words by: Martin Fjordvald
- September 12, 2010
- 7 Comments
- Next Post
-
-
Gregory Kornblum
Posted: December 12, 2010
Sorry for the delayed response, just following links from the NginX wiki...
Even now PHP is an infant in the world of OOP let alone the encapsulation concepts born from its ideology. Although the authors who write their parts in C++ know this concept well when your trying to add it to what started out as nothing more then a template language there are gonna be things like this.
Languages or their extensions in C's case need to decide from the get go what they are going to be or changes to that later on will be a rough undertaking and this is one side effect.
Examples are abound. Ruby/Python, both, Objective-C, just OOP, no namespaces, C++/Java/C#, just OOP, namespaces. PHP, uhh... template... uhh.. no wait, functional, uhhh... no, scap that, OOP, uhhh.... wait, OOP & namespaces.
See? ;)
-
Juan Manuel
Posted: February 8, 2011
It isn't an escape problem ?
$ php -r "var_dump('\Evil\Namespace');"
ilNamespace');"string(15) "\Evil\Namespace"
$ php -r "var_dump('\\Evil\\Namespace');"
string(15) "\Evil\Namespace"
Albert
Posted: September 20, 2010
Hi great article!
But it seems to work for me:
output:
string(5) "WORKS"
int(1)
string(5) "WORKS"
int(2)
string(5) "WORKS"
int(3)
string(5) "WORKS"
int(4)
PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:03:45)
Which version do you use?