Map-based autoloader across php and phar resources

⌈⌋ branch:  Canonic Autoloader


Artifact Content

Artifact c3111d55a2fcada8acc8f11e4e3dda460a0d8fb1:

Attachment "x.php" to wiki page [rfc:function_autoloading] added by mario 2014-02-15 02:24:58.
<?php
/**
 * 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);
    }
}