PHP code example of bogdanpet / orthite-di

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

    

bogdanpet / orthite-di example snippets




class User {

    public function __construct(Request $request, Database $db) {
        .
        .
        .
    }
}



class Database {

    public function __construct(\PDO $pdo) {
        .
        .
        .
    }
}



class Request {
        .
        .
        .
}

$request = new Request();
$pdo = new PDO($dsn, $username, $passwd);
$db = new Database($pdo);
$user = new User($request, $db);

$container = Orthite\DI\Container::getInstance();

$user = $container->get(User::class);

$user = $container->get(User::class, [
    'dsn' => $dsn,
    'username' => $username,
    'passwd' => $passwd
]);

$user = $container->get(User::class, compact('dsn', 'username', 'passwd');

// No previous instantiation of container is , 'username', 'passwd');



class User {

    public function __construct(Request $request, Database $db) {
        .
        .
        .
    }
    
    public function show(Response $response, $id) {
        .
        .
        .
        return $response(...$something);
    }
}

$container = Orthite\DI\Container::getInstance();
$response = $container->call(User::class, 'show', ['id' => 1]);
echo $response; // or whatever

$response = container_call(User::class, 'show', ['id' => 1]);
echo $response; // or whatever