Skip to content

Add exceptions for PHP native classes #2

@peterjaap

Description

@peterjaap
==> var/log/debug.log <==
[2025-11-11T11:55:57.105937+00:00] main.CRITICAL: Error: Cannot make instance of internal class lazy: Magento\Backend\Model\Menu\Interceptor inherits internal class ArrayObject in /data/web/releases/1762859154/vendor/mage-os/framework/ObjectManager/Factory/Compiled.php:55
Stack trace:
#0 /data/web/releases/1762859154/vendor/mage-os/framework/ObjectManager/Factory/Compiled.php(55): ReflectionClass->newLazyProxy()
#1 /data/web/releases/1762859154/vendor/mage-os/framework/ObjectManager/ObjectManager.php(59): Magento\Framework\ObjectManager\Factory\Compiled->create()
..  etc

Native PHP classes can't be made lazy so we need to add the list of classes to exempt from being made lazy;

**
 * Check if a class extends any internal PHP class
 */
private function isInternalClassExtension($className)
{
    $internalClasses = [
        'ArrayObject',
        'SplFixedArray',
        'SplHeap',
        'SplMinHeap',
        'SplMaxHeap',
        'SplPriorityQueue',
        'SplQueue',
        'SplStack',
        'SplFileInfo',
        'SplFileObject',
        'SplTempFileObject',
        'DirectoryIterator',
        'FilesystemIterator',
        'GlobIterator',
        'RecursiveDirectoryIterator',
        'RecursiveIteratorIterator',
        'AppendIterator',
        'InfiniteIterator',
        'IteratorIterator',
        'LimitIterator',
        'NoRewindIterator',
        'DateTime',
        'DateTimeImmutable',
        'DateTimeZone',
        'DateInterval',
        'DatePeriod',
        'Exception',
        'ErrorException',
        'Error',
        'PDO',
        'PDOStatement',
        'CURLHandle',
        'mysqli',
        'mysqli_stmt',
        'DOMElement',
        'DOMNode',
        'DOMDocument',
        'SimpleXMLElement',
        'SessionHandler',
    ];

    try {
        $reflClass = new \ReflectionClass($className);
        $parent = $reflClass->getParentClass();

        while ($parent) {
            $parentName = $parent->getName();
            if (in_array($parentName, $internalClasses)) {
                return true;
            }
            if ($parent->isInternal()) {
                return true;
            }
            $parent = $parent->getParentClass();
        }
    } catch (\ReflectionException $e) {
        return false;
    }

    return false;
}

public function create($requestedType, array $arguments = [])
{
    $finalClass = $this->config->getInstanceType($requestedType);

    if (\PHP_VERSION_ID >= 80400 &&
        !\str_ends_with($finalClass, '\\Proxy') &&
        \class_exists($finalClass) &&
        $arguments === [] &&
        !$this->isInternalClassExtension($finalClass)
    ) {
        return (new \ReflectionClass($finalClass))->newLazyProxy(
            function ($object) use ($requestedType, $arguments) {
                return $this->doCreate($requestedType, $arguments);
            }
        );
    }

    return $this->doCreate($requestedType, $arguments);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions