PHP code example of ifcanduela / container

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

    

ifcanduela / container example snippets


$container = new ifcanduela\container\Container([
    "alpha" => "a",
    "beta" => "b",
]);

$container->merge([
    "gamma" => "g",
    "delta" => "d",
]);

$container->set("db_username", "root@localhost");

echo $container->get("db_username");

$container["db_username"] = "root@localhost";

echo $container["db_username"];

use function ifcanduela\container\raw;

$container->raw("rand", function (int $max) {
    return random_int(0, $max);
});

// using the helper
$container->set("rand", raw(function (...) {...}));

// using array index notation
$container["rand"] = raw(function (...) {...});

$rand = $container->get("rand");

echo $rand(100);

$container->set("logger", function (Container $c) {
    return new Logger($c->get("log_path"));
});

// using array index notation
$container["logger"] = function (...) {...};

$logger = $container->get("logger");

$logger->log(Logger::INFO, "I'm the logger");

use function ifcanduela\container\factory;

$container->factory("rand", function (Container $c) {
    return random_int(0, $c->get("max_random_number"));
});

// using the helper
$container->set("rand", factory(function (...) {...}));

// using array index notation
$container["rand"] = factory(function (...) {...});

echo $container->get("rand"); // => 24
echo $container->get("rand"); // => 71
echo $container->get("rand"); // => 13

$container = new \ifcanduela\container\Container([
    "a" => 1,
    "b" => 2,
    "c" => 3,
    "e" => 5,
]);

var_dump($container->has("a")); // => true
var_dump($container->has("d")); // => false
var_dump(isset($container["d"])); // => false