1. Go to this page and download the library: Download lordmonoxide/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/ */
lordmonoxide / phi example snippets
class Foo {
public $bar = null;
public function __construct(Bar $bar) {
$this->bar = $bar;
}
}
$foo = $phi->make('Foo');
// $foo->bar = new Bar
class Foo {
public $a = null;
public $b = null;
public $first_name = null;
public $last_name = null;
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');
// $foo->a == new A
// $foo->b == new B
// $foo->first_name == null
// $foo->last_name == null
$foo = $phi->make('Foo', ['John', 'Doe']);
// $foo->a == new A
// $foo->b == new B
// $foo->first_name == 'John'
// $foo->last_name == 'Doe'
$foo = $phi->make('Foo', ['John']);
// $foo->a == new A
// $foo->b == new B
// $foo->first_name == 'John'
// $foo->last_name == null
$a = new A;
$foo = $phi->make('Foo', ['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', [$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', 'Bar');
$foo = $phi->make('Foo');
// $foo->bar == new Bar
$bar = $phi->make('BarInterface');
// $bar = new Bar
$phi->bind('A', 'B');
$a = $phi->make('A');
// $a == new B
$default_pdo = new PDO(...); // The default database
$stats_pdo = new PDO(...); // PDO pointing to a different database
$phi->bind('PDO', $default_pdo);