PHP code example of alicemajere / wonderland-container

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

    

alicemajere / wonderland-container example snippets


$container = new Wonderland\Container\Container();

// creating a service definition
$definition = new Wonderland\Container\Service\ServiceDefinition(
    'service.name',
    MyClass::class,
    ['args1', '@service.name2', ['array_args']],
    ['methodCall1' => ['args1', '@service.name3']]
);

// register the definition into the container
$container->addService($definition);

$instance = new MyClass();
$serviceInstance = new Wonderland\Container\Service\InstanceDefinition(
    'service.name2',
    $instance
);

// register the instance definition into the container
$container->addServiceInstance(serviceInstance);


if ($container->has('service.name')) {
    // code here
}

// retrieve the instance. Will create a new one if not created yet. Shared by default
$instance = $container->get('service.name');

// retrieve a new instance of the service. Setting the second parameters to true will not retrieve the shared service
 instance
$newInstance = $container->get('service.name', true);

// retrieve the instance of the instance service definition. Setting the second parameter to true will do nothing if 
// the service is not a definition service but a instance definition service; only the shared instance will be retrieved
$instance = $container->get('service.name2');
 php
$container = new \Wonderland\Container\ServiceContainer();
$serviceLoader = new \Wonderland\Container\Configuration\ServiceLoader(new YamlParser());
// load every yml in a folder
$container->loadServices($serviceLoader->loadDirectory(__DIR__ . '/Resource/'));
// or a single file
$container->loadServices($serviceLoader->loadFile(__DIR__ . '/Resource/test.yml'));