1

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.

MikeiLL
  • 6,282
  • 5
  • 37
  • 68
  • I think what gets returned by require / include is what is returned from the file it is requiring / including. I believe you are getting true because the autoloader you are including does not specify a return. IE true on success / false, error on failure. https://www.php.net/manual/en/function.include.php#example-128 – Alex Barker Oct 15 '20 at 17:53
  • Thanks, @AlexBarker. That is my understanding as well. But the specified autoloader _does_ return the object. And check my update, when I duplicate the file and require that one, it does return the expected object. – MikeiLL Oct 15 '20 at 17:56

1 Answers1

2

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 )
Paul
  • 139,544
  • 27
  • 275
  • 264