1. Go to this page and download the library: Download chipslays/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/ */
chipslays / container example snippets
use Please\Container\Container;
use Please\Container\Support\Getter;
$container = new Container;
$container->bind(Mailer::class, function (Getter $get) {
return new Mailer::($get('user'), $get('password', 'qwerty')); // qwerty - default value
});
/** @var Mailer */
$mailer = $container->get(Mailer::class, [
'user' => 'admin',
// and password `qwerty` (default value)
]);
use Please\Container\Container;
$container = new Container;
$container->singleton('random', fn () => rand());
echo $container->get('random'); // 1234567890
echo $container->get('random'); // 1234567890
use Please\Container\Container;
$container = new Container;
$container->bind(Foo::class);
$container->get(Foo::class); // ok
$container->get('foo'); // error
// if you pass a class string
// it is also automatically bind as `Foo::class`
$container->bind('foo', Foo::class);
$container->get(Foo::class); // ok
$container->get('foo'); // ok
// array of aliases
$container->bind([Foo::class, 'foo', 'another-foo-alias'], Foo::class);
$container->get(Foo::class); // ok
$container->get('foo'); // ok
$container->get('another-foo-alias'); // ok
$container->singleton('timestamp', fn () => time());
// or use bind class and `shared` parameter
$container->bind('timestamp', fn () => time(), shared: true);
use Please\Container\Container as BaseContainer;
use Please\Container\Support\Traits\Singleton;
class Container extends BaseContainer
{
use Singleton;
}
...
$container = Container::getInstance();
...
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.