Created
January 31, 2012 18:08
-
-
Save jamiel/1711933 to your computer and use it in GitHub Desktop.
PHP 5.3 namespaces have taken away the biggest advantage of autoloading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PHP 5.3 namespaces have taken away the biggest advantage of autoloading | |
----------------------------------------------------------------------- | |
When autoloading was introduced into PHP, the PHP world rejoiced as we no | |
longer needed to require every class we wanted to use in our current file | |
at the top of our code. With the introduction of namespaces this advantage | |
has been completely reversed, where unless you are writing all your code in | |
a single namespace (unlikely) you will end up needing to "use" every class | |
you want to include. | |
To back this up, let us take the Symfony2 framework as an example as of | |
today (31st January 2012). | |
jamiel@gentoo ~/git/symfony/src $ find . -name '*.php' | wc -l | |
1164 | |
So there are 1164 source files in the framework at the moment. | |
jamiel@gentoo ~/git/symfony/src $ egrep -r '^use ' * | wc -l | |
2331 | |
There are 2331 use statements so bearing in mind symfony 1.4 has | |
practically *no* fluff at the top of each class, namespaces have | |
added an extra 2331 new age require statements. | |
The top 10 all have 9 of these annoying declarations! | |
jamiel@gentoo ~/git/symfony/src $ egrep -rc '^use ' * | sed 's/\(.*\):\([0-9]\+\)/\2:\1/' | sort -r | head -n10 | |
9:Symfony/Component/Validator/ValidatorFactory.php | |
9:Symfony/Component/Security/Http/Firewall/RememberMeListener.php | |
9:Symfony/Component/Security/Core/Authentication/AuthenticationProviderManager.php | |
9:Symfony/Component/HttpKernel/Client.php | |
9:Symfony/Component/Form/Extension/Validator/Validator/DelegatingValidator.php | |
9:Symfony/Component/Form/Extension/Core/Type/TimeType.php | |
9:Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php | |
9:Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php | |
9:Symfony/Bundle/SecurityBundle/SecurityBundle.php | |
9:Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php | |
EDIT: It would seem I am not the first to find this annoying, and this has been discussed before | |
by none other than Mr. Zaninotto | |
http://propel.posterous.com/the-end-of-autoloading |
<?php
$obj = new Structure\MyClass;
$obj->helloWorld();
$obj = new Structure\MyClass2;
$obj->helloWorld();
So, seems that whole problem appeared just because you haven't RTFM? ;-)
use
doesn't import something into the current scope - it just creates alias for your namespace paths. That's it.
@everzet - Thanks, reverted the update seeing as that improvement exists.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I added a new section to the gist with some thoughts on how things can be improved (revision 728603). I still hate writing "use" statements, though.