1. Go to this page and download the library: Download fas/di 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/ */
fas / di example snippets
// Brand new container
$container = new Container;
// Load compiled container if present
$container = Container::load('/tmp/container.php');
// ->set(entryName, entryName | callback | null)
// (singleton by default)
$container->set(LoggerInterface::class, 'some_container_entry');
// abstract factory
$container->set(LoggerInterface::class, 'some_container_entry')->factory();
// lazy
$container->set(LoggerInterface::class, 'some_container_entry')->lazy(LoggerInterface::class);
// lazy abstract factory
$container->set(LoggerInterface::class, 'some_container_entry')
->lazy(LoggerInterface::class)
->factory();
// shorthands
$container->singleton(LoggerInterface::class, 'some_container_entry');
$container->factory(LoggerInterface::class, 'some_container_entry');
$container->lazy(LoggerInterface::class, 'some_container_entry');
// If entry MyLogger::class does not exist in the container,
// The class MyLogger::class will be instantiated.
$container->singleton(LoggerInterface::class, MyLogger::class);
// Custom factory method
$container->singleton(LoggerInterface::class, function () {
return new MyLogger;
});
// Any callable will do
$container->singleton(LoggerInterface::class, [MyLoggerFactory::class, 'create']);
// ->factory(entryName, entryName | callback | null)
$container->factory(MyLogger::class); // abstract factory shorthand
$logger1 = $container->get(MyLogger::class); // will create new object
$logger2 = $container->get(MyLogger::class); // will create new object
// Use cached virtual proxies (lazy), and write cache if missing
$container->enableProxyCache('/tmp/proxies');
// Generate a class containing proper methods for registered entries.
// This can be used afterwards to avoid a lot of reflection when resolving entries.
$container->save('/tmp/container.php');
$container = Container::load("/tmp/container.php");
if (!$container) {
$container = new Container;
$container->singleton(LoggerInterface::class, MyLogger::class);
$container->save('/tmp/container.php');
}
$container->enableProxyCache("/tmp/proxies");
// Container ready for use