PHP code example of crishellco / rivet-ioc

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

    

crishellco / rivet-ioc example snippets


namespace App;
class DbDriver
{
    ...
}

namespace App;
class Db
{
    protected $driver;

    public function __construct(DbDriver $driver)
    {
        $this->driver = $driver;
    }
}

namespace App\Services;
class UserService
{
    protected $db;

    public function __construct(Db $db)
    {
        $this->db = $db;
    }
}

$userService = \RivetIoc\Ioc::instance()->make('App\Services\UserService');

// Or use the helper function...
$userService = rivet_make('App\Services\UserService');

\RivetIoc\Ioc::instance()->register('App\Db', function() {
    $mysqli = new mysqli('localhost', 'username', 'password', 'mydb');
    $db = new App\Db($mysqli);

    return $db;
});

$db = \RivetIoc\Ioc::instance()->make('App\Db');

// Or use the helper function...
$db = rivet_make('App\Db');

register_shutdown_function(function() {
    \RivetIoc\Ioc::instance()->forget('App\Db');
});

namespace App\Services;
class UserService
{
    use \RivetIoc\Traits\Locator;

    protected $dao;

    public function __construct()
    {
        $this->dao = $this->make('App\Doa\UserDao');

        // Or use the helper function...
        $this->dao = rivet_make('App\Doa\UserDao');
    }
}