PHP code example of cora / container

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

    

cora / container example snippets



namespace Classes;

class UserManager {

  protected $db;

  public function __construct($db)
  {
    $this->db = $db;
  }

  public function fetch($id)
  {
    // do stuff
  }
} // end class


// Create a container
$container = new \Cora\Container();

// Define some resources on the container
$container->{\Classes\UserManager::class} = function ($c) {
  return new \Classes\UserManager(
    $c->{\Classes\Database::class}()
  );
};

$container->{\Classes\Database::class} = function ($c) {
  return new \Classes\Database();
};

// Grab an instance of UserManager, letting the container handle injection of the database class
$users = $container->{\Classes\UserManager::class}();
$users->fetch($id);



// Create a container
$container = new \Cora\Container();

// Define database connection as singleton so it will return the same object every time.
$container->singleton(\Classes\DatabaseConnection::class, function($c) {
  $config = 


namespace Classes;

class UserManager {

  protected $db;
  protected $repo;

  public function __construct($db, $userRepository)
  {
    $this->db = $db;
    $this->repo = $userRepository;
  }

  public static function di_config($c)
  {
    return new \Classes\UserManager(
      $c->{\Classes\Database::class}(),
      $c->{\Classes\UserRepository::class}()
    );
  }

  public function fetch($id)
  {
    // do stuff
  }
} // end class


namespace Classes;

class Database {

  protected $conn;

  public function __construct($connection)
  {
    $this->conn = $connection;
  }

  public static function di_config($c)
  {
    return new \Classes\Database(
      $c->{\Classes\DatabaseConnection::class}()
    );
  }
} // end class


namespace Classes;

class UserRepository {

  protected $gateway;
  protected $factory;

  public function __construct($gateway, $factory)
  {
    $this->gateway = $gateway;
    $this->factory = $factory;
  }

  public static function di_config($c)
  {
    return new \Classes\UserRepository(
      $c->{\Classes\UserGateway::class}(),
      $c->{\Classes\UserFactory::class}()
    );
  }
} // end class


namespace Classes;

class UserGateway {

  protected $db;

  public function __construct($db)
  {
    $this->db = $db;
  }

  public static function di_config($c)
  {
    return new \Classes\UserGateway(
      $c->{\Classes\Database::class}()
    );
  }
} // end class


namespace Classes;

class UserFactory {

  public function __construct()
  {
    // stuff
  }

  public static function di_config($c)
  {
    return new \Classes\UserFactory();
  }
} // end class

$databaseConfig = on = new \Classes\DatabaseConnection($databaseConfig);
$users = new \Classes\UserManager(
  new \Classes\Database($databaseConnection),
  new \Classes\UserRepository(
    new \Classes\UserGateway(
      new \Classes\Database($databaseConnection)
    ),
    new \Classes\UserFactory()
  )
);
$users->fetch($id);


namespace Classes;

class User {

  public $name;

  public function __construct($name)
  {
    $this->name = $name;
  }

  public static function di_config($c, $name)
  {
    return new \Classes\User($name);
  }
} // end class


// Create a container
$container = new \Cora\Container();

$user = $container->{\Classes\User::class}('Bob');
echo $user->name; // Outputs "Bob"


namespace Classes;

class UserManager {

  protected $factory;

  public function __construct($userFactory)
  {
    $this->factory = $userFactory
  }

  public static function di_config($c)
  {
    return new \Classes\UserManager(
      $c->getFactory(\Classes\User::class)
    );
  }

  public function create($name)
  {
    return $this->factory->make($name);
  }
} // end class


namespace Classes;

class User {

  public $name;

  public function __construct($name)
  {
    $this->name = $name;
  }
  
  public static function di_config($c, $name)
  {
    return new \Classes\User($name);
  }
}

// Create a container
$container = new \Cora\Container();
$users = $container->{\Classes\UserManager::class}();
$user = $users->create('John');
echo $user->name; // Outputs "John"

$container->{\Classes\SomeClass::class}($arg1, $arg2)