Nette\DI\Container::createInstance PHP Метод

createInstance() публичный Метод

Creates new instance using autowiring.
public createInstance ( $class, array $args = [] ) : object
$args array
Результат object
    public function createInstance($class, array $args = [])
    {
        $rc = new \ReflectionClass($class);
        if (!$rc->isInstantiable()) {
            throw new ServiceCreationException("Class {$class} is not instantiable.");
        } elseif ($constructor = $rc->getConstructor()) {
            return $rc->newInstanceArgs(Helpers::autowireArguments($constructor, $args, $this));
        } elseif ($args) {
            throw new ServiceCreationException("Unable to pass arguments, class {$class} has no constructor.");
        }
        return new $class();
    }

Usage Example

 /**
  * Creates new presenter instance.
  *
  * @param  string presenter class name
  * @return Application\IPresenter
  */
 public function createPresenter($class)
 {
     $callInjects = $this->alwaysCallInjects;
     $services = array_keys($this->container->findByTag('nette.presenter'), $class);
     if (count($services) > 1) {
         throw new Application\InvalidPresenterException("Multiple services of type {$class} found: " . implode(', ', $services) . '.');
     } elseif (count($services)) {
         $presenter = $this->container->createService($services[0]);
         $callInjects = FALSE;
     } elseif (count($services = $this->container->findByType($class)) === 1) {
         $presenter = $this->container->createService($services[0]);
     } else {
         $presenter = $this->container->createInstance($class);
         $callInjects = TRUE;
     }
     if (!$presenter instanceof Application\IPresenter) {
         throw new UnexpectedValueException("Unable to create create presenter, returned value is not Nette\\Application\\IPresenter type.");
     }
     if ($callInjects) {
         $this->container->callInjects($presenter);
     }
     if ($presenter instanceof Application\UI\Presenter && $presenter->invalidLinkMode === NULL) {
         $presenter->invalidLinkMode = $this->invalidLinkMode;
     }
     return $presenter;
 }
All Usage Examples Of Nette\DI\Container::createInstance