phpcomposer-phprequirerequire-once

require_once returns true instead of expected object


Both vendor/autoload.php files are basically identical:

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit1e055ec2de42b90d5e1653b608643281::getLoader();

In both files:

$composer_object = ComposerAutoloaderInit1e055ec2de42b90d5e1653b608643281::getLoader();
var_dump($composer_object);
> object(Composer\Autoload\ClassLoader

This has had me digging into Composer's autoload files (which has been insightful), but I'm still perplexed:

$var = require_once '/srv/path_to_main/vendor/autoload.php';
var_dump($var);
> boolean true

$var = require_once '/srv/path_to_subdir/vendor/autoload.php';
var_dump($var);
> object(Composer\Autoload\ClassLoader

Why does requiring the first file return boolean true, and not object(Composer\Autoload\ClassLoader?

Update

I created a copy of vendor/autoload.php, vendor/aught.php and when I require that one, it returns the object.


Solution

  • require_once returns true instead of the return value if the file has already been included or required.

    You'll find you get true for the subdir one as well if you require it twice:

    require_once '/srv/path_to_subdir/vendor/autoload.php';
    $var = require_once '/srv/path_to_subdir/vendor/autoload.php';
    var_dump( $var )