use Zend\ServiceManager\ServiceLocatorInterface; class MyClass { protected $serviceLocator; public function __construct(ServiceLocatorInterface $serviceLocator) { $this->serviceLocator = $serviceLocator; } public function doSomething() { // Get a logger service from the service locator $logger = $this->serviceLocator->get('Logger'); // Use the logger to log a message $logger->info('Something happened!'); } }
use Symfony\Component\DependencyInjection\ContainerInterface; class MyController extends AbstractController { protected $container; public function __construct(ContainerInterface $container) { $this->container = $container; } public function index() { // Get a service from the container $myService = $this->container->get('App\Service\MyService'); // Do something with the service $myService->doSomething(); // Render a template return $this->render('my_template.html.twig'); } }In this example, we inject a `ContainerInterface` object into `MyController` through its constructor. Then, in the `index()` method, we retrieve a `MyService` service from the container and use it to do some work. Finally, we render a template and return it. Overall, the `ServiceLocatorInterface` is a useful interface for accessing services in a service container. It is commonly used in PHP frameworks and libraries such as Zend, Symfony, and Laravel.