Search code examples
laravelcomposer-phphelperautoloader

Why laravel helpers accessible from all namespaces?


So far i see that there are two file with functions that loaded with composer autoloader.

 "autoload": {
         "files": [
             "src/Illuminate/Foundation/helpers.php",
             "src/Illuminate/Support/helpers.php"
        ],

And then in autoloader:

function composerRequire69685de7f834ebe45f1e02416f8679f0($fileIdentifier, $file)
{
    if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
        require $file;

        $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
    }
}

But actually it just require this file, and all functions comes only to global namespace. Can you explain me why eventually all this functions are accessible from all namespaces?


Solution

  • This is why helpers works in all namespaces:

    For functions and constants, PHP will fall back to global functions or constants if a namespaced function or constant does not exist.

    PHP docs