Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Instantiator.php

function Instantiator::instantiate

Same name in this branch
  1. 11.1.x vendor/doctrine/instantiator/src/Doctrine/Instantiator/Instantiator.php \Doctrine\Instantiator\Instantiator::instantiate()

Creates an object and sets its properties without calling its constructor nor any other methods.

@template T of object

Parameters

class-string<T> $class The class of the instance to create:

array<string, mixed> $properties The properties to set on the instance:

array<class-string, array<string, mixed>> $scopedProperties The properties to set on the instance,: keyed by their declaring class

Return value

T

Throws

ExceptionInterface When the instance cannot be created

See also

Hydrator::hydrate() for examples

File

vendor/symfony/var-exporter/Instantiator.php, line 41

Class

Instantiator
A utility class to create objects without calling their constructor.

Namespace

Symfony\Component\VarExporter

Code

public static function instantiate(string $class, array $properties = [], array $scopedProperties = []) : object {
    $reflector = Registry::$reflectors[$class] ??= Registry::getClassReflector($class);
    if (Registry::$cloneable[$class]) {
        $instance = clone Registry::$prototypes[$class];
    }
    elseif (Registry::$instantiableWithoutConstructor[$class]) {
        $instance = $reflector->newInstanceWithoutConstructor();
    }
    elseif (null === Registry::$prototypes[$class]) {
        throw new NotInstantiableTypeException($class);
    }
    elseif ($reflector->implementsInterface('Serializable') && !method_exists($class, '__unserialize')) {
        $instance = unserialize('C:' . \strlen($class) . ':"' . $class . '":0:{}');
    }
    else {
        $instance = unserialize('O:' . \strlen($class) . ':"' . $class . '":0:{}');
    }
    return $properties || $scopedProperties ? Hydrator::hydrate($instance, $properties, $scopedProperties) : $instance;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal