PHP code example of terrydjony / ion

1. Go to this page and download the library: Download terrydjony/ion 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/ */

    

terrydjony / ion example snippets


use Ion\Container;

$ion = new Container();

$stdClass = $ion->make('stdClass');

// $stdClass is an instance of stdClass

$ion->register('PDO', function() {
    return new PDO('mysql:host=localhost;dbname=databasename','root','');
};

$pdo = $ion->make('PDO');

// $pdo is an instance of PDO class with the arguments as defined before

$config = array('dsn'=>'mysql:host=localhost;dbname=databasename','username'=>'root','password'=>'');

$ion->register('PDO', function() use ($config) {
    return new PDO($config['dsn'],$config['username'],$username['password']);
});

$pdo = $ion->make('PDO');
And, notice that the first parameter of register is a key. The key can be anything, but it's recommended to use the class name as the key.

$ion->register('DAL', function() {
    return new PDO($config['dsn'],$config['username'],$config['password']);
};

$dal = $ion->make('DAL');
// $dal is a PDO object

$ion->registerFactory('CarFactory', function() {
    return new Car();
};

$car1 = $ion->make('CarFactory');
$car2 = $ion->make('CarFactory');

// $car1 and $car2 are different objects
If you want to use the supplied arguments, you can use getArgs() method.

$this->ion->registerFactory('User', function($ion) {
  return new User($ion->getArgs());
});

$user1 = $this->ion->make('User',array('Foo'));
// $user1 is an instance with argument Foo

$ion->setParam('db_dsn','mysql:host=localhost;dbname=databasename');
$ion->setParam('db_user','root');
$ion->setParam('db_password','');

$ion->register('PDO', function($ion) {
    return new PDO($ion->param('db_dsn'), $ion->param('db_user'), $ion->param('db_pass');
}

// register a PDO service before

$pdo1 = $ion->makeNew('PDO');
$pdo2 = $ion->makeNew('PDO');

// $pdo1 and $pdo2 are different objects no matter how the PDO service is registered 

class A
{
    public function __construct(DatabaseAdapterInterface $dba, StandardClassInterface $stdClass, StandardInterface $obj)
    {

    }
}

$ion->register('PDO', function() {
    return new PDO('mysql:host=localhost;dbname=databasename','root','');
}

// Binds DatabaseAdapterInterface to PDO class which has been registered
$ion->bindInterface('DatabaseAdapterInterface', 'PDO');

// Binds StandardClassInterface to stdClass which needs no arguments for construction
$ion->bindInterface('StandardClassInterface', 'stdClass');

$a = $ion->make('A');

class A
{
  public function __construct(B $b)
  {

  }
}

class B
{
  public function __construct(C $c)
  {

  }
}

class C
{
  public function __construct()
  {
    
  }
}

$a = $ion->make('A');

class A
{
    public function __construct(B $b, $name)
    {
        echo "Hello ".$name
    }
}

class B
{
    public function __construct(DatabaseAdapterInterface $dba)
    {

    }
}


$ion->register('PDO', function() {
    return new PDO('mysql:host=localhost;dbname=databasename','root','');
});
$ion->bindInterface('DatabaseAdapterInterface','PDO');

$a = $ion->make('A',array('World'));