yii\di\Container::setSingleton PHP Method

setSingleton() public method

This method is similar to Container::set except that classes registered via this method will only have one instance. Each time Container::get is called, the same instance of the specified class will be returned.
See also: set()
public setSingleton ( string $class, mixed $definition = [], array $params = [] )
$class string class name, interface name or alias name
$definition mixed the definition associated with `$class`. See [[set()]] for more details.
$params array the list of constructor parameters. The parameters will be passed to the class constructor when [[get()]] is called.
    public function setSingleton($class, $definition = [], array $params = [])
    {
        $this->_definitions[$class] = $this->normalizeDefinition($class, $definition);
        $this->_params[$class] = $params;
        $this->_singletons[$class] = null;
        return $this;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * @return Container
  */
 private static function getIoc()
 {
     $diConfig = (include 'di.php');
     $container = new Container();
     $container->setSingleton('workerMapper', $diConfig['workerMapper']);
     $container->setSingleton('entityfx\\utils\\workers\\contracts\\repositories\\WorkerRepositoryInterface', function ($container, $params, $config) use($diConfig) {
         return $container->get($diConfig['workerRepository'], [$container->get('workerMapper')]);
     });
     $container->set('entityfx\\utils\\workers\\contracts\\WorkerInterceptorInterface', function ($container, $params, $config) use($diConfig) {
         return $container->get($diConfig['workerInterceptor'], [$params['worker'], $container->get('entityfx\\utils\\workers\\contracts\\repositories\\WorkerRepositoryInterface')]);
     });
     return $container;
 }
All Usage Examples Of yii\di\Container::setSingleton