PHP code example of lordmonoxide / phi

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) {
    // ...
  }
}

$a = new A;
$b = new B;
$foo = $phi->make('Foo', [$b, 'last_name' => 'Doe', $a]);
// $foo->a == $a
// $foo->b == $b
// $foo->first_name == null
// $foo->last_name  == 'Doe'

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

$phi->bind('Person', function() {
  return new Person('John', 'Doe');
});

$person = $phi->make('Person');
// $person->first_name == 'John'
// $person->last_name  == 'Doe'

$id = 0;

$phi->bind('Person', function() use(&$id) {
  return new Person(++$id);
});

$person1 = $phi->make('Person'); // id == 1
$person2 = $phi->make('Person'); // id == 2

$id = 0;

$phi->bind('Person', function($name, $age) use(&$id) {
  $names = explode(' ', $name);
  return new Person(++$id, $name[0], $name[1], $age);
});

$person = $phi->make('Person', ['John Doe', 21]);
// $person->id == 1
// $person->first_name == 'John'
// $person->last_name  == 'Doe'
// $person->age == 21

$default_pdo = new PDO(...); // The default database
$stats_pdo   = new PDO(...); // PDO pointing to a different database

$phi->bind('PDO', $default_pdo);

$pdo = $phi->make('PDO');
// $pdo == $default_pdo

class Table {
  public function __construct(PDO $pdo, $table_name) {
    // ...
  }
}

$users_table = $phi->make('Table', ['users']);
// $users_table->pdo   == $default_pdo
// $users_table->table == 'users'

$stats_table = $phi->make('Table', [$stats_pdo, 'stats']);
// $stats_table->pdo   == $stats_pdo
// $stats_table->table == 'stats'

$phi->bind('core.log', 'Vendor\Package\Core\Logging\Log');

$log = $phi->make('core.log');
// $log == new Vendor\Package\Core\Logging\Log

use LordMonoxide\Phi\ResolverInterface;

class CustomResolver implements ResolverInterface {
  public function make($alias, array $arguments = []) {
    if($alias == 'A') {
      return new B(new A());
    }
  }
}

$phi->addResolver(new CustomResolver());

$b = $phi->make('A');
//$b == new B

use Illuminate\Support\Facades\App;
use LordMonoxide\Phi\ResolverInterface;

class LaravelResolver implements ResolverInterface {
  public function make($alias, array $arguments = []) {
    if(App::bound($alias)) {
      return App::make($alias, $arguments);
    }
  }
}

$phi->addResolver(new LaravelResolver());