1. Go to this page and download the library: Download lychee/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/ */
lychee / container example snippets
use Lychee\Container;
$container = new Container;
class Foo
{
public function sayHello()
{
echo 'Hello from Foo' . PHP_EOL;
}
}
// 直接绑定实例
$container->bind('foo', new Foo());
// 也可以传递类的名字
$container->bind('foo', Foo::class);
// 还可以通过返回值是类实例的闭包函数
$container->bind('foo', function () {
return new Foo;
});
$container->singleton('foo', new Foo());
// 参数跟 bind() 都是一样的,下面不再赘述
$foo = $container->make('foo');
$foo->sayHello();
class Foo
{
private $bar;
public function __construct(Bar $bar)
{
$this->bar = $bar;
}
public function sayHello()
{
echo 'Hello, I\'m ' . $this->bar->getName() . PHP_EOL;
}
}
class Bar
{
public function getName(): string
{
return 'Y!an';
}
}
$container->bind('bar', new Bar());
$container->bind('foo', Foo::class);
$foo = $container->make('foo');
$foo->sayHello(); // Hello, I'm Y!an
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.