PHP code example of php-strict / container

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

    

php-strict / container example snippets


use PhpStrict\Container\Container

$container = new Container();
$container->set('key1', 1);

if ($container->has('key1')) {
    $var1 = $container->get('key1');
}

use PhpStrict\Container\Container

$myObject = new stdClass();

$container = new Container();
$container->setByRef('key2', $myObject);

$anotherObject =& $container->getRef('key2');

use PhpStrict\Container\Container

$container = new Container([
    'key1' => 1,
    'key2' => 'value 2',
    'key3' => true,
]);

if ($container->has('key2')) {
    $var2 = $container->get('key2');
}

use PhpStrict\Container\Container

class MyClass
{
    protected $field1;
    protected $field2;
    protected $field3;
    
    public function unpacker(array $entries): void
    {
        foreach ($entries as $key => $value) {
            $this->$key = $value;
        }
    }
}

$container = new Container([
    'field1' => 1,
    'field2' => 'value 2',
    'field3' => true,
]);

$myClassObject = new MyClass();

$container->unpackWith([$myClassObject, 'unpacker']);
bash
composer