Skip to content

Instantly share code, notes, and snippets.

@jamiel
Created January 31, 2012 18:08
Show Gist options
  • Save jamiel/1711933 to your computer and use it in GitHub Desktop.
Save jamiel/1711933 to your computer and use it in GitHub Desktop.
PHP 5.3 namespaces have taken away the biggest advantage of autoloading
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
@jamiel
Copy link
Author

jamiel commented Feb 1, 2012

@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