PHP code example of dikki / ci4-di

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

    

dikki / ci4-di example snippets




namespace Config;

use CodeIgniter\Config\BaseService;
use CodeIgniter\Config\App;
use Dikki\Container\Application;

class Services extends BaseService
{
    // ...

    public static function codeigniter(?App $config = null, bool $getShared = true)
    {
        if ($getShared) {
            return static::getSharedInstance('codeigniter', $config);
        }

        $config = $config ?? config('App');

        return new Application($config);
    }
}

class Home extends BaseController
{
    public function __construct(
        private UserService $userService
    ) {}
}

// app/Config/Container.php
public array $services = [
    // Interface binding
    'App\Interfaces\UserRepoInterface' => 'App\Repositories\UserRepo',
    
    // Concrete value or class
    'mailer' => 'App\Services\Mailer',
];

public array $shared = [
    // Singleton services
    'db.connection' => 'App\Database\Connection',
];

// app/Config/Container.php
public array $providers = [
    'App\Providers\AppServiceProvider',
];

use Dikki\Container\Container;

$container = Container::getInstance();
$service = $container->get('my_service');
bash
php spark container:publish