Map-based autoloader across php and phar resources

⌈⌋ branch:  Canonic Autoloader


Artifact [08e479d70a]

Artifact 08e479d70a348a0f024bd705dc151b2b04d1ee3e:

Wiki page [rfc:function_autoloading] by mario 2014-02-15 02:26:06.
D 2014-02-15T02:26:06.902
L rfc:function_autoloading
N text/x-markdown
P ef6c7fc51b01d8eb8834377e53a8774ccdb573b9
U mario
W 2768
<h3> Function autoloading </h3>

PHP is a *hybrid* language. It however unevenly implements autoloading for classes only as of now. There are long-standing feature requests for supporting it with all language symbols. A more recent attempt to bring some systematization here was:

See [https://wiki.php.net/rfc/function_autoloading](https://wiki.php.net/rfc/function_autoloading)

Of course, PHP ain't Python, and functions aren't first class language properties; despite PHPs actual more scripty features and pioneering in that regard.

While technical assessing did crop up, recent discussions actually circled around preferences. Cargo culters aren't pleased with consolidating language semantics for *second-rate* coding paradigms. The groupthink being "Only class-wrapped code is quality code". (Casual alluding to phpcl***es.org here in the parens for no reason whatsoever.)


### Why is it in shared.phar ?

It's a proof-of-concept mostly. 

Implementing function declaration lookups is a *trivial* by-product of doing things properly anyway.


### How can I use it *now*?

For current PHP versions, you need a workaround like this to shoehorn globally-autoloaded functions into your code:

        /**
         * Userland workaround for function_autoloading.
         * Implements a static class, which random global functions can be
         * invoked with.
         *
         *    x::autoload_me(1,2,3);
         *
         */
         
        class x {
            function __callStatic($name, $args=array()) {
                if (!function_exists($name)) {

                    static $ca;

                    // find suitable autoloaders
                    if (!isset($ca)) {
                        $ca = array();
                        
                        foreach (array_filter("is_object", spl_autoload_functions()) as $spl) {
                            if (is_a($spl, "Canonic_Autoloader")) {
                                $ca[] = $spl;
                            }
                        }
                    }

                    // invoke each autoloader
                    foreach ($ca as $autoload) {
                        $autoload($name, 2);
                    }
                }

                return call_user_func_array($name, $args);
            }
        }

Which obviously is hardly as enchanting as having PHP adhere to its hybrid heritage. And `x::` while an acceptably readable prefix, is not a standard denomination for such a feature.

Though the bemusing part here is, you just need to place `x.php` in your classpath. Then `shared.phar` will autoload both subsequently; first the static `x::` class, then your `autoloaded_func()` prior calling it.
Z c9ec33ffdb85e371069e4f47c659777b