PHP code example of echo-fusion / servicemanager

1. Go to this page and download the library: Download echo-fusion/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/ */

    

echo-fusion / servicemanager example snippets


use EchoFusion\ServiceManager\ServiceManager;
use EchoFusion\ServiceManager\Exceptions\ServiceManagerException;

$serviceManager = new ServiceManager();

// Register a service with an ID and class name (shared as singleton)
$serviceManager->set(MyServiceInterface::class, MyService::class, true);

// Register a service with a factory (non-singleton)
$serviceManager->set(AnotherServiceInterface::class, function (ServiceManagerInterface $sm) {
    return new AnotherService($sm->get(MyServiceInterface::class));
});

// Retrieve services
$myService = $serviceManager->get(MyServiceInterface::class);
$anotherService = $serviceManager->get(AnotherServiceInterface::class);

// Check if a service exists
if ($serviceManager->has(MyServiceInterface::class)) {
    // ...
}

use EchoFusion\ServiceManager\DefaultContainer;
use EchoFusion\ServiceManager\Providers\ServiceManagerProvider;
use EchoFusion\ServiceManager\ServiceManagerInterface;

// Initialize a container, falling back to DefaultContainer if $container is not provided
$container = $container ?? new DefaultContainer();

// Initialize the ServiceManagerProvider with the container
$provider = new ServiceManagerProvider();
// Register the ServiceManager within the container
$provider->register($container);
// Define and boot service configurations
$config = etMessage();
}
 bash
$ composer