1. Go to this page and download the library: Download agashe/sigmaphp-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/ */
agashe / sigmaphp-container example snippets
igmaPHP\Container\Container;
use MyApp\MailerService;
$container = new Container();
$container->set(MailerService::class);
if ($container->has(MailerService::class)) {
$mailerService = $container->get(MailerService::class);
}
use MyApp\MailerService;
use MyApp\DbConnection;
class UserModel
{
private $conn;
private $mailer;
public function __construct()
{
$this->conn = new DbConnection();
$this->mailer = new MailerService();
}
}
class UserModel
{
private $conn;
private $mailer;
public function __construct(DbConnection $conn, MailerService $mailer)
{
$this->conn = $conn;
$this->mailer = $mailer;
}
}
class UserModel
{
private $conn;
private $mailer;
public function __construct()
{}
public function setDbConnection(DbConnection $conn)
{
$this->conn = $conn;
}
public function setMailerService(MailerService $mailer)
{
$this->mailer = $mailer;
}
}
class UserModel
{
private $conn;
private $mailer;
public function __construct(DbConnection $conn, MailerService $mailer)
{
$this->conn = $conn;
$this->mailer = $mailer;
}
}
// Shape class
class Shape
{
private $height;
private $width;
private $length;
public function __construct($height, $width, $length = 30)
{
$this->height = $height;
$this->width = $width;
$this->length = $length;
}
}
// set in the container
$container = new Container();
$container->set(Shape::class)
->setParam('height', 10)
->setParam('width', 20);
$container->set(DoEverything::class)
->setParam('my_function', fn() => true)
->setParam('an_array', ['a', 'b', 'c'])
->setParam('error', (new \Exception()))
->setParam('count', 100);
// UserModel example
$container->set(UserModel::class)
->setParam('conn', function() {
return new DbConnection();
})
->setParam('mailer', function() {
return new MailerService();
});
// Shape class
class Shape
{
private $height;
private $width;
private $length;
public function __construct($height, $width, $length = 30)
{
$this->height = $height;
$this->width = $width;
$this->length = $length;
}
public function setDrawHandler(DrawHandler $handler)
{
$this->height = $height;
$this->width = $width;
$this->length = $length;
}
}
// bind Shape class to the container
$container = new Container([
DrawHandler::class => DrawHandler::class
Shape::class => [
'definition' => Shape::class,
'params' => [
'height' => 10,
'width' => 20
'length' => 30
],
'methods' => [
'setDrawHandler' => []
]
])
]);
$container = new Container();
$container->set('create_cube', function () {
$height = 5;
$width = 5;
$length = 5;
return (new Shape($height, $width, $length));
});
var_dump($container->get('create_cube')); // Shape
$container = new Container();
$container->set('db_connection', function ($host, $db, $user, $pass) {
return (new \PDO("mysql:host=$host;dbname=$db", $user, $pass));
})
->setParam('host', 'localhost')
->setParam('db', 'test')
->setParam('user', 'root')
->setParam('pass', 'root');
var_dump($container->get('db_connection')); // PDO
class Calculator
{
public function add($a, $b)
{
return $a + $b;
}
}
// somewhere in the app
$container = new Container();
$container->set(Calculator::class);
$result = $container->call(Calculator::class, 'add', [
'a' => 5,
'b' => 6,
]);
var_dump($result); // 11