PHP code example of johnroyer / php-easy-container

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

    

johnroyer / php-easy-container example snippets


use \Zeroplex\Container\Container;

$c = new Container;

// set value as singleton
$c['pi'] = 3.14159;
$c['db'] = new PDO(
    'mysql:dbname=mydb;host=localhost',
    'user',
    'password'
);

$c['pi'];  // 3.14159
$c['db'];  // object(PDO)

$c->singleton('db') = new PDO(
    'mysql:dbname=mydb;host=localhost',
    'user',
    'password'
);

$c->get('db');  // object(PDO)

$c->singleton('config') = new Config(
    APP_ROOT . '/config'
);

$c['config']['app.name'];

$c['db'] = function () {
    return new PDO(
        'mysql:dbname=test;host=localhost',
        'user',
        'password'
    );
};

$c->get('db');  // new PDO if and only if someone get it
                // object(PDO)

$c->provide('pi', 3.14159265359);
$c['pi'];  // 3.14159265359

$c->provide('nowDate', function() {
    // always return special format of date
    return (new DateTime())
        ->format('Y-m-d');
});
$c->['nowDate'];  // "2017-12-01"
bash
composer