On 2011-08-14, Derick Rethans <der...@php.net> wrote: > On Sat, 6 Aug 2011, Ferenc Kovacs wrote: > > I would like to introduce this RFC which would provide function > > autoloading through the spl_autoload facility without userland BC > > breakage. > > > > https://wiki.php.net/rfc/autofunc > > I understand the proposal, but I don't see any compelling reasons in the > RFC of why we actually need autoloading for functions? For classes it > makes sense because there is almost always a class to file mapping. For > functions (and constants) that is not the case, so I am wondering how > useful this function autoloading actually is.
Namespaces support the following code types: * Constants * Functions * Classes and Interfaces Currently, autoloading in PHP allows us to dynamically identify and load only classes and interfaces. However, not all code is written using OOP. If a developer doesn't want to dive into include/require(_once) hell in order to use their namespaced functions and/or constants, they have no real options. As an example, let's say I've created a procedural CMS or blog of some sort. In there, I have a file like this: <?php namespace MyProjectName\Posts; $entries = getEntries(date('Y')); foreach ($entries as $entry) { displayEntrySummary($entry); } Next, assume the "getEntries" and "displayEntriesSummary" functions are also in the namespace "MyProjectName\Posts". How does my code load those functions? Traditionally, we've used: include_once 'MyProjectName/Posts/functions.php'; or include_once __DIR__ . '/functions.php'; in our files. But why should we have to do that? Why are functions -- which make up the bulk of PHP's functionality, and which are the bread-and-butter of many PHP projects -- get second-class status when it comes to autoloading? I can see an autoloader like this: function autoloadFunctions($funcname, $type = SPL_AUTOLOAD_FUNCTION) { if (!strstr($funcname, '\\')) { // we won't deal with un-namespaced functions return false; } $namespace = substr($funcname, 0, strrpos($funcname, '\\')); $funcfile = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR . 'Functions.php'; return include_once($funcfile); } and that might autoload all functions in a given namespace. I've seen other projects that put a function per-file -- so those might become even more granular. Basically, I don't see why we _wouldn't_ want this functionality in PHP. It makes namespaces practical and useful for procedural applications. -- Matthew Weier O'Phinney Project Lead | matt...@zend.com Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php