PHP code example of anax / di

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

    

anax / di example snippets


// Create it
$di = new \Anax\DI\DI();

// Check its a PSR-11 interface
assert($di instanceof \Psr\Container\ContainerInterface);

// Add a service
$di->set("response", "\Anax\Response\Response");

// Add a shared service
$di->setShared("view", "\Anax\View\ViewContainer");

// Check if service is loaded
if ($di->has("view")) {
    ; // the service is loaded
}

// Get and use a service
$response = $di->get("response");
$response->addBody($body)->send();

// Same, without storing in a variable
$di->get("response")->addBody($body)->send();

// Add a shared service
$di->setShared("view", "\Anax\View\ViewContainer");

// Get two instances of the shared service
$view1 = $di->get("view");
$view2 = $di->get("view");
assert($view1 === $view2);

// Add a service
$di->set("response", "\Anax\Response\Response");

// Get two instances of the service
$response1 = $di->get("response");
$response2 = $di->get("response");
assert($response1 !== $response2);

// Add services
$di->set("response", "\Anax\Response\Response");
$di->setShared("view", "\Anax\View\ViewContainer");

// Get one service
$response = $di->get("response");

// Check what services are loaded
implode(",", $di->getServices()); // response,view

// Check what services are active
implode(",", $di->getActiveServices()); // response

// Add all framework services to $di
$di = new Anax\DI\DIFactoryConfig();
$di->loadServices(ANAX_INSTALL_PATH . "/config/di");

/**
 * Configuration file for request service.
 */
return [
    // Services to add to the container.
    "services" => [
        "request" => [
            "shared" => true,
            "callback" => function () {
                $obj = new \Anax\Request\Request();
                $obj->init();
                return $obj;
            }
        ],
    ],
];

"callback" => "\Anax\Request\Request",

"response" => [
    "shared" => true,
    //"callback" => "\Anax\Response\Response",
    "callback" => function () {
        $obj = new \Anax\Response\ResponseUtility();
        $obj->setDI($this);
        return $obj;
    }
],

"configuration" => [
    "shared" => true,
    "callback" => function () {
        $config = new \Anax\Configure\Configuration();
        $dirs = 

"session" => [
    "active" => defined("ANAX_WITH_SESSION") && ANAX_WITH_SESSION, // true|false
    "shared" => true,
    "callback" => function () {
        $session = new \Anax\Session\Session();

        // Load the configuration files
        $cfg = $this->get("configuration");
        $config = $cfg->load("session");

        // Set various settings in the $session
        // ...

        return $session;
    }
],

/**
 * Config-file for sessions.
 */
return [
    // Session name
    "name" => preg_replace("/[^a-z\d]/i", "", __DIR__),
];