PHP code example of codeburner / container

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

    

codeburner / container example snippets


use Codeburner\Container\Container;

$container = new Container;

// Regiser a "stdClass" class to a "std" key.
$container->set('std', 'stdClass');

// Accessing new "stdClass" objects.
$container->get('std');

class ClassA {
	public function __construct(stdClass $dependency) {

	}
}

$container->set('my-a', 'ClassA');

if ($container->has('my-a')) {
	$container->get('my-a');
}

$container->set('app.model.posts', App\Model\Post::class);

$obj1 = $container->get('app.model.posts'); // App\Model\Post#1
$obj2 = $container->get('app.model.posts'); // App\Model\Post#2

// you can define by passing a third parameter to set
$container->set('database', App\Database::class, true);
// or using the `singleton` method
$container->singleton('database', App\Database::class);

$obj1 = $container->get('database'); // App\Database#1
$obj2 = $container->get('database'); // App\Database#1

class ClassNameTest {

}

$container->set('someobj', ClassNameTest::class);

$container->set('someobj', function ($container) {
	$obj = new stdClass;
	$obj->attribute = 1;

	return $obj;
});

$obj = new stdClass;

// you can set instances directly by the set method
$container->set('std', $obj);
// or use the `instance` method
$container->instance('std', $obj);

class Post {
	public function __construct(Category $category) {
		$this->category = $category;
	}
}

class Category {
	public function __construct() {
		$this->id = rand();
	}
}

$post = $container->make(Post::class);

echo $post->category->id;

class Post {
	public function __construct(Category $category) {
		$this->category = $category;
	}
}

class Category {
	public function __construct() {
		$this->id = rand();
	}
}

$category = new Category;
$category->id = 1;

$container->setTo(Post::class, Category::class, $category);
$post = $container->make(Post::class);

echo $post->category->id; // 1

$post = $container->make(Post::class, [Category::class => new Category], true);

$container->call(function (User $user, Posts $posts) {
    // ...
});

$container->call(function (User $user, Posts $posts) {}, [User::class => new User]);

$container->set('app.services.mail', App\Services\MailService::class);

$container->extend('app.services.mail', function ($instance, $container) {
	$instance->environment('development');

    $instance->setHtmlWrapper($container->get('app.wrappers.html'));

	return $instance;
});