PHP code example of codeinc / services-manager
1. Go to this page and download the library: Download codeinc/services-manager library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codeinc / services-manager example snippets
use CodeInc\ServicesManager\ServicesManager;
use CodeInc\ServicesManager\ServiceInterface;
// a first service
class MyFirstService implements ServiceInterface
{
public function hello(string $name):void
{
echo sprintf("Hello %s!", $name);
}
}
// a second service using the first service
class MySecondService implements ServiceInterface
{
/** @var MyFirstService */
private $myFirstClass;
public function __construct(MyFirstService $myFirstClass)
{
$this->myFirstClass = $myFirstClass;
}
public function helloWorld():void
{
$this->myFirstClass->hello("World");
}
}
// calling the second service, the service manager is going to first instantiated MyFirstService
// then instantiate MySecondService with MyFirstService as a parameter.
$serviceManager = new ServicesManager();
$mySecondService = $serviceManager->getService(MySecondService::class);
$mySecondService->helloWorld();
// you also can add external objects to makes them available to the servides,
// for instance a PSR-7 ServerRequest object or Doctrine's EntityManager.
$serviceManager->addService($entityManager);
// the service manager will pass the instance of the service manager added
// using addService()
class MyThirdService {
public function __construct(EntityManager $entityManager) { }
}