1. Go to this page and download the library: Download petrknap/php-servicemanager 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/ */
petrknap / php-servicemanager example snippets
// classes.php
class MyDatabase
{
public function __construct($dsn, $user, $password) {/* ... */}
}
class MyWeb
{
public function __construct(MyDatabase $database) {/* ... */}
}
class MyBlog
{
public function __construct(MyWeb $web) {/* ... */}
public function show($page) {/* ... */}
}
class MyAdmin
{
public function __construct(MyWeb $web) {/* ... */}
public function show($page) {/* ... */}
}
// index.php
"config.php");
$database = new MyDatabase($config["dsn"], $config["username"], $config["password"]);
$web = new MyWeb($database);
$blog = new MyBlog($web);
$blog->show("homepage");
// admin.php
"config.php");
$database = new MyDatabase($config["dsn"], $config["username"], $config["password"]);
$web = new MyWeb($database);
$admin = new MyAdmin($web);
$admin->show("dashboard");
// classes.php
use PetrKnap\Php\ServiceManager\ServiceManager;
use Psr\Container\ContainerInterface;
class MyDatabase
{
public function __construct($dsn, $user, $password) {/* ... */}
}
class MyWeb
{
public function __construct(MyDatabase $database) {/* ... */}
}
class MyBlog
{
public function __construct(MyWeb $web) {/* ... */}
public function show($page) {/* ... */}
}
class MyAdmin
{
public function __construct(MyWeb $web) {/* ... */}
public function show($page) {/* ... */}
}
ServiceManager::setConfig([
"factories" => [
"MyDatabase" => function() {
$config =
// index.php
use PetrKnap\Php\ServiceManager\ServiceManager;
// admin.php
use PetrKnap\Php\ServiceManager\ServiceManager;
use PetrKnap\Php\ServiceManager\ConfigurationBuilder;
use PetrKnap\Php\ServiceManager\ServiceManager;
use Psr\Container\ContainerInterface;
class MyCoreClass
{
/* ... */
}
class MyClass
{
private $core;
public function __construct(MyCoreClass $core)
{
$this->core = $core;
}
}
$configBuilder = new ConfigurationBuilder();
$configBuilder->addInvokable("MyCoreClass", "MyCoreClass");
$configBuilder->setShared("MyCoreClass", true);
$configBuilder->addFactory("MyClass", function(ContainerInterface $container) {
return new MyClass($container->get("MyCoreClass"));
});
ServiceManager::setConfig($configBuilder);
use PetrKnap\Php\ServiceManager\ServiceManager;
$serviceManager = ServiceManager::getInstance();
$myClass = $serviceManager->get("MyClass");