1. Go to this page and download the library: Download rs-world/instantiator 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/ */
rs-world / instantiator example snippets
class DatabaseIntantiator extends Instantiator
{
protected function register()
{
$this->instance([
"default" => function($a, $b) {
return new \Path\To\Database($a, $b);
},
"test" => function($a, $b) {
return new \Path\To\FakeDatabase($a, $b);
}
]);
}
public function get(A $a, B $b): \Path\To\DatabaseInterface
{
return $this->getInstance($a, $b);
}
}
// in default mode
$dbi = new DatabaseInstantiator();
$db = $dbi->get($a, $b);
var_dump($db instanceof \Path\To\Database); // prints true
// in test mode
$dbi = new DatabaseInstantiator("test", true);
$db = $dbi->get($a, $b);
var_dump($db instanceof \Path\To\DatabaseFake); // prints true
// set global mode to "test"
Instantiator::setGlobalMode("test");
// now
$dbi = new DatabaseInstantiator();
$db = $dbi->get($a, $b);
var_dump($db instanceof \Path\To\Database); // prints false
var_dump($db instanceof \Path\To\DatabaseFake); // prints true
class ServiceInstantiator extends Instantator
{
protected function register()
{
// ...
// ...
$dbi = new \Path\To\DatabaseInstantiator(
$this->getMode(),
$this->getFallback()
);
$db = $dbi->getInstance($a, $b);
$this->instance([
"default" => function($x) use($db) {
return \Path\To\Service($db, $x);
}
]);
}
public function get($x): ServiceInterface
{
return $this->getInstance($x);
}
}