PHP code example of jhonymiler / di

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

    

jhonymiler / di example snippets


    use Tests\Core\Container;
    stance();

    // Binding a customized MysqlDb instance to the container
    $container->bind(MysqlDb::class, function () {
        // Custom configuration for MysqlDb, if needed
        $db = new Db(/*...*/);
        return new MysqlDb($db);
    });

    // Retrieving an instance of MysqlDb from the container
    $mysqlDb = $container->get(MysqlDb::class);
    // Retrieving an instance of User from the container
    $user = $container->get(User::class);

    // Using the call method to automatically resolve dependencies for a closure
    $container->call(function (Logger $logger, EntityManager $em) use ($user) {
        // Here, the logUserData method of the User class will be called with resolved dependencies
        $user->logUserData($logger);
        $data = $em->getData();
        var_dump($data);
    });

    try {
        $invalidInstance = $container->get(NonExistentClass::class);
    } catch (Exception $e) {
        // Handle the exception here
    }