Repository: symfony/symfony
File: src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php (PHP)

Code before:
        ContainerBuilder $container,
        FileLocatorInterface $locator,
        ?string $env = null,
        private ?ConfigBuilderGeneratorInterface $generator = null,
        bool $prepend = false,
    ) {
        parent::__construct($container, $locator, $env, $prepend);
    }

    public function load(mixed $resource, ?string $type = null): mixed
    {
        // the container and loader variables are exposed to the included file below
        $container = $this->container;
        $loader = $this;

        $path = $this->locator->locate($resource);
        $this->setCurrentDir(\dirname($path));
        $this->container->fileExists($path);

        // Force load ContainerConfigurator to make env(), param() etc available.
        class_exists(ContainerConfigurator::class);

        $autoloaderRegistered = false;
        if (null === self::$configBuilderAutoloader) {
            $generator = $this->generator;
            self::$configBuilderAutoloader = function (string $class) use ($loader, $generator) {
                if (!$generator || !str_starts_with($class, 'Symfony\\Config\\') || !str_ends_with($class, 'Config')) {
                    return;
                }

                try {
                    $loader->configBuilder($class);
                } catch (\LogicException|\InvalidArgumentException) {
                    // no-op
                }
            };
            spl_autoload_register(self::$configBuilderAutoloader);
            $autoloaderRegistered = true;
        }

        // the closure forbids access to the private scope in the included file
        $load = \Closure::bind(static function ($path, $env) use ($container, $loader, $resource, $type) {
            return include $path;
        }, null, null);

        try {
            try {
                if (1 === $result = $load($path, $this->env)) {
                    $result = null;
                }
            } catch (\Error $e) {

Review comment (suggestion):
You use a static variable but the closure contains instances of `$loader` and `$generator` that can vary between 2 instances of `PhpFileLoader`. I don't have a use-case for calling an other `PhpClassLoader` inside `PhpClassLoader::load`, but that sounds fragile to rely on a global state. Making `$configBuilderAutoloader` non-static seems more correct to me, with the same result.

Also, `$loader = $this` and `$generator = $this->generator`. You don't need to pass this variables, the closure is bound to the current class, you can call `$this->configBuilder($class)`.

Code after:
        ContainerBuilder $container,
        FileLocatorInterface $locator,
        ?string $env = null,
        private ?ConfigBuilderGeneratorInterface $generator = null,
        bool $prepend = false,
    ) {
        parent::__construct($container, $locator, $env, $prepend);
    }

    public function load(mixed $resource, ?string $type = null): mixed
    {
        // the container and loader variables are exposed to the included file below
        $container = $this->container;
        $loader = $this;

        $path = $this->locator->locate($resource);
        $this->setCurrentDir(\dirname($path));
        $this->container->fileExists($path);

        // Force load ContainerConfigurator to make env(), param() etc available.
        class_exists(ContainerConfigurator::class);

        if ($autoloaderRegistered = !$this->configBuilderAutoloader && $this->generator) {
            spl_autoload_register($this->configBuilderAutoloader = function (string $class) {
                if (str_starts_with($class, 'Symfony\\Config\\') && str_ends_with($class, 'Config')) {
                    $this->configBuilder($class);
                }
            });
        }

        // the closure forbids access to the private scope in the included file
        $load = \Closure::bind(static function ($path, $env) use ($container, $loader, $resource, $type) {
            return include $path;
        }, null, null);

        try {
            try {
                if (1 === $result = $load($path, $this->env)) {
                    $result = null;
                }
            } catch (\Error $e) {
                $load = \Closure::bind(function ($path, $env) use ($container, $loader, $resource, $type) {
                    return include $path;
                }, $this, ProtectedPhpFileLoader::class);

                if (1 === $result = $load($path, $this->env)) {
                    $result = null;
                }

                trigger_deprecation('symfony/dependency-injection', '8.1', 'Using `$this` or its internal scope in config files is deprecated, use the `$loader` variable instead in "%s" on line %d.', $e->getFile(), $e->getLine());
            }