1. Go to this page and download the library: Download sugiphp/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/ */
sugiphp / container example snippets
$container = new Container();
// store a parameter
$container->set("param", "value");
// store an object
$container->set("pdo", function() {
return new PDO("mysql:dbname=testdb;host=127.0.0.1", "user", "pass");
});
$container->get("param"); // returns "value"
$container->get("unset"); // will throw an NotFoundException
$db = $container->get("pdo"); // returns instance of a PDO (not the closure itself, but the result);
// later in a code...
$db1 = $container->get("pdo"); // returns the SAME instance of the PDO (not new instance!) ($db1 === $db)
// if you need a new instance of the PDO you can force it with factory() method
$db2 = $container->factory("pdo"); // returns new instance of the PDO.
// the second instance is not stored in a container, so if you use factory again
$db3 = $container->factory("pdo"); // you'll get third instance which is different from the instances above
$db4 = $container->get("pdo"); // will return same instance as the first one ($db4 === $db === $db1)
// Wrap closure in factory method
$container->set("rand", $container->factory(function() {
return mt_rand();
}));
$rand1 = $container->get("rand");
$rand2 = $container->get("rand");
// both values will differ (unless your are extremely lucky)
$closure = $container->raw("pdo"); // this will return the closure, not the result
// so you can invoke it and make a new PDO instance
$db = $closure();
$container->set("name", $container->raw(function() {
return "John";
}));
is_string($container->get("name")); // FALSE
// actually it will return stored closure
// set a "name"
$container->set("name", "John");
$container->get("name"); // "John"
// override a "name"
$container->set("name", "John Doe");
$container->get("name"); // "John Doe"
// lock a key
$container->lock("name");
// now if you try to override "name"
$container->set("name", "Foo Bar"); // will throw ContainerException
// or try to delete that key
$container->delete("name"); // will throw ContainerException
$container["foo"] = "bar";
echo $container["foo"]; // prints bar
$container["pdo"] = function () {
return new PDO("mysql:dbname=testdb;host=127.0.0.1", "user", "pass");
};
$db = $container["pdo"]; // returns instance of the PDO class
// checking for existence
isset($container["foo"]); // TRUE
// delete a key
unset("foo");
// checking for existence
isset($container["foo"]); // FALSE
$container->set("foo") = "bar";
$container["foo"] = "bar";
$container->foo = "bar";
// All of the above are doing the same job
$container->get("foo"); // "bar"
$container["foo"]; // "bar"
$container->foo; // "bar"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.