An alternative (which I haven't properly developed yet). Thoughts?

```
define_autoload(AUTOLOAD_NS, function($ns){
    if($ns === '\A\B\C'){
        echo "LOADING ABC\n";
        require(__DIR__ . $ns . '\functions_and_constants.php');
    }
});

define_autoload(AUTOLOAD_NS, function($ns){
    if($ns === '\A\B'){
        echo "LOADING AB\n";
        require(__DIR__ . $ns . '\functions_and_constants.php');
    }
});

define_autoload(AUTOLOAD_NS, function($ns){
    if($ns === '\B'){
        echo "LOADING A\n";
        require(__DIR__ . $ns . '\functions_and_constants.php');
    }
});

\A\B\someFunction();
// prints:
// LOADING A\n
// LOADING AB\n

\A\B\someFunction();
// tries no autoload, because both namespaces A and A\B were
// attempted to be loaded already.
// in other words, autoload is attempted only once
// for any given namespace

\A\B\C\someOtherFunction();
// prints:
// LOADING ABC\n
// because contrarily to super namespaces, A\B\C wasn't
// attempted to be loaded yet
```

Reply via email to