It does however not check if the scope actually exists.
public isScopeActive ( string $name ) : boolean | ||
$name | string | |
return | boolean |
use Symfony\Component\DependencyInjection\ContainerInterface; class MyController { private $container; public function __construct(ContainerInterface $container) { $this->container = $container; } public function foo() { if ($this->container->isScopeActive('request')) { // do something } } }
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; $container = new ContainerBuilder(); $container->register('my_service', 'MyService') ->setScope('request'); if ($container->isScopeActive('request')) { $myService = $container->get('my_service'); }In this example, the ContainerBuilder is used to create a container with a registered service that has the 'request' scope. The isScopeActive method is used to check if the 'request' scope is currently active before getting the service. Package Library: Symfony DependencyInjection Component
public isScopeActive ( string $name ) : boolean | ||
$name | string | |
return | boolean |