skip to Main Content

I use local webserver XAMPP + WordPress on Windows 10. PHP v.8.2.0, Composer v.2.5.8.

My actions:

  1. Installed Composer globally and some package just for initial test to random directory.
  2. Copied "vendor" folder to child theme directory(wordpresswp-contentthemestwentytwentythree-child) and
    included autoload.php in functions.php. Made some tests – good, everything works.

Then I deleted almost all files from vendor folder and made some code manipulations. In result, I have folder structure:

twentytwentythree-child/
├── vendor/
│   ├── composer/
│   │   └── autoload_real.php
│   └── autoload.php    
├── style.css
└── functions.php  

functions.php:

require_once __DIR__ . "/vendor/autoload.php";

And this code in autoload_real.php:

class ComposerAutoloaderInit205eb07b158c0f9d7b54b8e32a48421e
{
    private static $loader;

    public static function getLoader()
    {
        self::$loader = $loader = new ComposerAutoloadClassLoader(dirname(__DIR__));

        print "<pre>";
        print_r($loader);
        print "</pre>";

        return $loader;
    }
}

Without any problems I got the printing result on site page:

ComposerAutoloadClassLoader Object
(
    [vendorDir:ComposerAutoloadClassLoader:private] => D:ProgWebXAMPPwwwwordpresswp-contentthemestwentytwentythree-childvendor
    [prefixLengthsPsr4:ComposerAutoloadClassLoader:private] => Array
        (
        )

    [prefixDirsPsr4:ComposerAutoloadClassLoader:private] => Array
        (
        )

    [fallbackDirsPsr4:ComposerAutoloadClassLoader:private] => Array
        (
        )

    [prefixesPsr0:ComposerAutoloadClassLoader:private] => Array
        (
        )

    [fallbackDirsPsr0:ComposerAutoloadClassLoader:private] => Array
        (
        )

    [useIncludePath:ComposerAutoloadClassLoader:private] => 
    [classMap:ComposerAutoloadClassLoader:private] => Array
        (
        )

    [classMapAuthoritative:ComposerAutoloadClassLoader:private] => 
    [missingClasses:ComposerAutoloadClassLoader:private] => Array
        (
        )

    [apcuPrefix:ComposerAutoloadClassLoader:private] => 
)

There is no ClassLoader.php in my folder structure and other vendor autload files, but this code works, and I don’t understand why.

This code doesn’t work on other machine, so I thought about Composer cache, tried to clear it with "composer clear-cache" cli command. I tried change local web server.Then I even uninstalled Composer and pointed out to remove all cache too. No effect, printing still works, I see the same output on WordPress page. So if you know, how without almost all vendor files and even Composer program itself, this code still works with no errors, tell me please.

2

Answers


  1. Chosen as BEST ANSWER

    Okay, I found the reason - some WordPress plugin also uses Composer autoloader with all its files and etc. ClassLoader.php was included by this plugin, i.e. my experimental code works. Big thanks to @hakre for hinting me the right way.


  2. How autoloading classes works even after uninstalled Composer?

    This works because once you have successfully build (installed, by the composer install or composer update command) the vendor folder, it is self-containing and can run without/independent from Composer only depending on the PHP platform configuration you’ve used installing it.

    The rest is in the vendor folder then, which includes the generated autoloader.

    Therefore autoloading classes works even after you uninstalled Composer, because it was independent to it in the first place. This is by the design of Composer, you only use it to manage your project dependencies, not to run them.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search