function AutowiredInstanceTrait::createInstanceAutowired
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/DependencyInjection/AutowiredInstanceTrait.php \Drupal\Core\DependencyInjection\AutowiredInstanceTrait::createInstanceAutowired()
Instantiates a new instance of the implementing class using autowiring.
Parameters
\Symfony\Component\DependencyInjection\ContainerInterface $container: The service container this instance should use.
mixed ...$args: Any predefined arguments to pass to the constructor.
Return value
static
5 calls to AutowiredInstanceTrait::createInstanceAutowired()
- AutowireTrait::create in core/
lib/ Drupal/ Core/ DependencyInjection/ AutowireTrait.php - Instantiates a new instance of the implementing class using autowiring.
- FormatterBase::create in core/
lib/ Drupal/ Core/ Field/ FormatterBase.php - Instantiates a new instance of the implementing class using autowiring.
- OverrideMenuLinks::create in core/
lib/ Drupal/ Core/ Menu/ Plugin/ ConfigAction/ OverrideMenuLinks.php - Creates an instance of the plugin.
- PluginBase::create in core/
lib/ Drupal/ Core/ Plugin/ PluginBase.php - Instantiates a new instance of the implementing class using autowiring.
- WidgetBase::create in core/
lib/ Drupal/ Core/ Field/ WidgetBase.php - Instantiates a new instance of the implementing class using autowiring.
File
-
core/
lib/ Drupal/ Core/ DependencyInjection/ AutowiredInstanceTrait.php, line 27
Class
- AutowiredInstanceTrait
- Defines a base trait for automatically wiring dependency arguments.
Namespace
Drupal\Core\DependencyInjectionCode
public static function createInstanceAutowired(ContainerInterface $container, mixed ...$args) : static {
$reflection = new \ReflectionClass(static::class);
if (method_exists(static::class, '__construct')) {
$parameters = array_slice($reflection->getMethod('__construct')
->getParameters(), count($args));
$args = array_merge($args, self::getAutowireArguments($container, $parameters, '__construct'));
}
$instance = new static(...$args);
foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
if (!empty($method->getAttributes(Required::class))) {
$method->invoke($instance, ...self::getAutowireArguments($container, $method->getParameters(), $method->getName()));
}
}
return $instance;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.