1. Go to this page and download the library: Download top-cat-software/tcsf-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/ */
top-cat-software / tcsf-di example snippets
use TCSF\DI\Container;
// Get the container instance
$container = Container::instance();
// Create an instance with autowiring
$myService = $container->create(MyService::class);
use TCSF\DI\Container;
// Bind an interface to a concrete implementation
$container = Container::instance();
$container->bind(MyInterface::class, MyImplementation::class);
// Now you can create instances of the interface
$instance = $container->create(MyInterface::class);
// $instance will be an instance of MyImplementation
use TCSF\DI\Container;
// Bind a class as a singleton
$container = Container::instance();
$container->singleton(MyService::class, MyService::class);
// Get the same instance each time
$instance1 = $container->create(MyService::class);
$instance2 = $container->create(MyService::class);
// $instance1 === $instance2
use TCSF\DI\Container;
// Bind a class to a factory function
$container = Container::instance();
$container->bind(MyService::class, function($container) {
return new MyService('custom', 'parameters');
});
// Create an instance using the factory
$instance = $container->create(MyService::class);
use TCSF\DI\Container;
// Create an instance manually
$myService = new MyService('custom', 'parameters');
// Register it with the container
$container = Container::instance();
$container->registerInstance(MyService::class, $myService);
// Get the same instance back
$instance = $container->create(MyService::class);
// $instance === $myService
class UserRepository
{
// Implementation...
}
class UserService
{
private UserRepository $repository;
public function __construct(UserRepository $repository)
{
$this->repository = $repository;
}
}
// The container will automatically create a UserRepository when creating a UserService
$userService = $container->create(UserService::class);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.