PHP code example of tyler / envase

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

    

tyler / envase example snippets



// Static definitions
// simple key => val pairs
$defs = [
    'foo' => 'bar'
    'config' => [
        'dir' => '/path/to/something/'
    ],
    'Session' => new Session
];

// Create container with defs
$c = new Envase\Container($defs);

// Add additional defs
$c->set('new', 'value');

// retrieve item
$item = $c->get('foo');

// $item will equal 'bar'

$c->set('foo', fn(Envase\Container $c) => new Bar);

// or the short hand helper
$c->set('foo', Envase\get(Bar::class))

$bar = $c->get('foo'); // <-- will result in calling the closure

class Foo 
{
    public function __construct(
        public Bar $bar,
        public string $dirToSomething
        
    )
}

class Bar
{
}

$c = new Envase\Container([
    'dirToSomething' => '/path/to/somewhere'
]);

$foo = $c->get(Foo::class);

class Foo 
{
    #[Inject]
    private Bar $bar; // will auto inject instance of Bar

    #[Inject]
    private string $foo // Will auto inject key foo, value will be 'bar'

    #[Inject("fizz")]
    private string $something // Will auto inject key fizz, value will be 'buzz'
}

class Bar
{
}

$c = new Envase\Container([
    'foo' => 'bar',
    'fizz' => 'buzz'
]);

$foo = $c->get(Foo::class); // will be instance of Foo

$foo = $c->make(Foo::class);