1. Go to this page and download the library: Download bapcat/phi 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/ */
bapcat / phi example snippets
class Foo {
public $bar = null;
public function __construct(Bar $bar) {
$this->bar = $bar;
}
}
$foo = $phi->make(Foo::class);
// $foo->bar = new Bar
class Foo {
public $a;
public $b;
public $first_name;
public $last_name;
public function __construct(B $b, A $a, $first_name = null, $last_name = null) {
$this->a = $a;
$this->b = $b;
$this->first_name = $first_name;
$this->last_name = $last_name;
}
}
$foo = $phi->make(Foo::class);
// $foo->a == new A
// $foo->b == new B
// $foo->first_name == null
// $foo->last_name == null
$foo = $phi->make(Foo::class, ['John', 'Doe']);
// $foo->a == new A
// $foo->b == new B
// $foo->first_name == 'John'
// $foo->last_name == 'Doe'
$foo = $phi->make(Foo::class, ['John']);
// $foo->a == new A
// $foo->b == new B
// $foo->first_name == 'John'
// $foo->last_name == null
$a = new A;
$foo = $phi->make(Foo::class, ['John', 'Doe', $a]);
// $foo->a == $a
// $foo->b == new B
// $foo->first_name == 'John'
// $foo->last_name == 'Doe'
class Foo {
public function __construct(BarInterface $bar, A $a, BarInterface $baz, B $b) {
// ...
}
}
$bar = new Bar; // implements BarInterface
$baz = new Baz; // implements BarInterface
$foo = $phi->make(Foo::class, [$bar, $baz]);
// $foo->bar == $bar
// $foo->baz == $baz
// $foo->a == new A
// $foo->b == new B
class Foo {
public function __construct(A $a, B $b, $first_name = null, $last_name = null) {
// ...
}
}
interface BarInterface {
}
class Bar implements BarInterface {
}
class Foo {
public function __construct(BarInterface $bar) {
// ...
}
}
$phi->bind(BarInterface::class, Bar::class);
$foo = $phi->make(Foo::class);
// $foo->bar == new Bar
$bar = $phi->make(BarInterface::class);
// $bar = new Bar
$phi->bind(A::class, B::class);
$a = $phi->make(A::class);
// $a == new B
$default_pdo = new PDO(...); // The default database
$stats_pdo = new PDO(...); // PDO pointing to a different database
$phi->bind(PDO::class, $default_pdo);
$phi->bind('core.log', \Vendor\Package\Core\Logging\Log::class);
$log = $phi->make('core.log');
// $log == new Vendor\Package\Core\Logging\Log
use BapCat\Interfaces\Ioc\Resolver;
class CustomResolver implements Resolver {
public function make($alias, array $arguments = []) {
if($alias == A::class) {
return new B(new A());
}
}
}
$phi->addResolver(new CustomResolver());
$b = $phi->make(A::class);
//$b == new B
use Illuminate\Support\Facades\App;
use BapCat\Interfaces\Ioc\Resolver;
class LaravelResolver implements Resolver {
public function make($alias, array $arguments = []) {
if(App::bound($alias)) {
return App::make($alias, $arguments);
}
}
}
$phi->addResolver(new LaravelResolver());
$ioc->bind(FooInterface::class, Foo::class);
$ioc->bind('bap.foo', FooInterface::class);
### Singletons
While it's possible to bind an alias to an instance of a class, effectively creating a singleton, it's not always
desirable (or possible) to load all singletons at boot time. Instead, singletons can be registered and lazy-loaded.