1. Go to this page and download the library: Download ranyuen/di 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/ */
ranyuen / di example snippets
Ranyuen\Di\Container;
class Momonga
{
public $id;
public function __construct($id = '')
{
$this->id = $id;
}
}
$c = new Container();
$c['id'] = 'Sample ID.';
$c['momonga'] = function ($c) { return new Momonga($c['id']); };
$c['factory'] = $c->factory(function ($c) { return new Momonga(); });
var_dump('Sample ID.' === $c['id']);
var_dump($c['momonga'] instanceof Momonga);
var_dump($c['momonga'] === $c['momonga']);
var_dump('Sample ID.' === $c['momonga']->id);
var_dump($c['factory'] instanceof Momonga);
var_dump($c['factory'] !== $c['factory']);
$c = new Container();
$c['momonga'] = function ($c) { return new Momonga(); };
class Yuraru
{
public $benri;
public $id;
/** @Inject */
public $momonga;
public function __construct($momonga, $id)
{
$this->benri = $momonga;
$this->id = $id;
}
}
// We can pass additional args.
$yuraru = $c->newInstance('Yuraru', ['Sample ID.']);
var_dump($yuraru->benri instanceof Momonga);
var_dump('Sample ID.' === $yuraru->id);
var_dump($yuraru->momonga instanceof Momonga);
$c = new Container();
$c['momonga'] = function ($c) { return new Momonga(); };
class Gardea
{
/** @Inject */
public $momonga;
}
$gardea = new Gardea();
$c->inject($gardea);
var_dump($gardea->momonga instanceof Momonga);
$c = new Container();
$c->bind('Momonga', 'momonga', function ($c) { return new Momonga(); });
class Benri
{
/**
* @Inject
* @var Momonga
*/
public $benri;
public $momonga;
public function __construct(Momonga $benri)
{
$this->momonga = $benri;
}
}
$benri = $c->newInstance('Benri');
var_dump($benri->benri instanceof Momonga);
var_dump($benri->momonga instanceof Momonga);
$c = new Container();
$c['momonga'] = function ($c) { return new Momonga(); };
class Musasabi
{
/**
* @Inject
* @Named(musasabi=momonga)
*/
public $musasabi;
public $benri;
/** @Named(benri=momonga) */
public function __construct($benri)
{
$this->benri = $benri;
}
}
$musasabi = $c->newInstance('Musasabi');
var_dump($musasabi->benri instanceof Momonga);
var_dump($musasabi->musasabi instanceof Momonga);
$c = new Container();
$c->bind('Momonga', 'momonga', function ($c) { return new Momonga(); });
$c->bind('Momonga', 'factory', $c->factory(function ($c) { return new Momonga(); }));
class MomongaFactory
{
/** @Inject */
public $momonga;
/** @Inject */
public $factory;
}
$momonga = $c->newInstance('MomongaFactory');
var_dump($momonga->momonga instanceof Momonga);
var_dump($momonga->momonga === $c['momonga']);
var_dump($momonga->factory instanceof Momonga);
var_dump($momonga->factory !== $c['factory']);
class Building
{
public function launch()
{
return 'rocket';
}
}
$c = new Container();
Container::setAsFacade($c);
$c['building'] = function ($c) { return new Building(); };
$c->facade('Station', 'building');
var_dump('rocket' === Station::launch());
class Monday
{
public function sunday($day = 1)
{
return $day;
}
public function tuesday($day = 2)
{
return $day;
}
}
$c = new Container();
$c->wrap('Monday', ['tuesday'], function ($invocation, $args) {
$day = $args[0];
return $invocation($day + 1);
});
$c->wrap('Monday', ['/day$/'], function ($invocation, $args) {
$day = $args[0];
return $invocation($day * 7);
});
$monday = $c->newInstance('Monday');
var_dump(1 * 7 === $monday->sunday());
var_dump(2 * 7 + 1 === $monday->tuesday());