PHP code example of felixgro / fgdi

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

    

felixgro / fgdi example snippets


use FGDI\Container;

// Create a container instance. This should be done once in your application.
$container = new Container();

// Register a value of any type directly in the container
$container->set('foo', 'bar');
$value = $container->get('foo'); // returns 'bar'

$container->set('baz', 42);
$value = $container->get('baz'); // returns 42

$container->set('nestedStuff', [
    'complex' => ['nested' => 'stuff'],
]);
$value = $container->get('nestedStuff'); // you guess it!

// Register classes automatically and resolve all nested dependencies needed
$container->set(AuthService::class);
$authService = $container->get(AuthService::class);

// If you want more control over the instantiation, you can pass a factory callback which returns an instanciated service
$container->set(CustomService::class, function (Container $container) {
    return new CustomService($container->get(NestedService::class));
});

// Conveniently register a singleton service
$container->singleton(DatabaseConnection::class);
$db1 = $container->get(DatabaseConnection::class);
$db2 = $container->get(DatabaseConnection::class);
// $db1 and $db2 are the exact same instance

// Check if a service is registered in the container.
$hasDC = $container->has(DatabaseConnection::class);
// This even returns true if the service is not yet registered but instantiable by the container