On Sat, Aug 31, 2013 at 11:57 PM, Vartolomei Nicolae <nvartolo...@gmail.com>wrote:
> On Saturday, August 31, 2013 at 9:03 PM, Sebastian Krebs wrote: > > > I already _have_ create files for functions of a namespace... Closed > source. > Can we take a look at them as an example? Maybe we can give you some advice > how to refactor this code :) > > So you will create files for constants? One file with a single line defining a constant? It seems like many people in this thread can't even imagine that there are reasonable schemes for function autoloading. So let me give you a practical autoloading scheme as an example. I have a library (https://github.com/nikic/iter) - it's not important what it does, just that it's function based -, which uses the namespaces iter, iter\fn and iter\rewindable. All functions (and classes!) belonging to the respective namespaces are stored in iter.php, iter.fn.php and iter.rewindable.php. [*] An autoloader for this scheme (according to current proposal) would look like this: php\autoload_register(function($name, $type) { if (0 !== strpos($name, 'iter\\')) return; // extract namespace portion of name $namespace = substr($name, 0, strrpos($name, '\\')); require __DIR__ . '/' . str_replace('\\', '.', $namespace) . '.php'; }, php\AUTOLOAD_FUNCTION | php\AUTOLOAD_CLASS); As you can see, this is a simple autoloader, verify similar to what you'd write for classes. Differences being a) only the namespace is used to locate the file, rather than the full name and b) I'm using . instead of /, but that's really irrelevant here. Furthermore note that the above autoloading function loads both functions and classes with the same scheme. As such I find it useful to specify something like FUNCTION|CLASS a the type. So, hopefully this clarifies that autoloading does not require "every function in its own file" or something like that. "Every namespace in its own file" works fine. This is somewhat akin to how Python modules are structured. Thanks, Nikita [*] This is not exactly true. There are functions located in the "wrong" file, but only due to the lack of autoloading. With autoloading it would be no problem to put all functions in the file belonging to their namespace.