PHP code example of maplephp / container

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

    

maplephp / container example snippets


use MaplePHP\Container\Container;
$container = new Container();
$container->set("YourClass", \YourNamespace\To\YourClass::class); // Bind "YourClass" to container and dependency injector
$yourClass = $container->get("YourClass")->get(); // Will return "YourClass"
//$yourClass->yourClassMehthod();

$container->factory("factoryKey", function() {
    $a = new TestClassA();
    $b = new TestClassB();
    return new TestClassC($a, $b);
});
echo $container->get("factoryKey"); // Will return TestClassC


$container->set("YourClass", \YourNamespace\To\YourClass::class);
$testService = $container->get("YourClass");
echo $testService->start();


namespace YourNamespace\To;

use YourNamespace\ToTestClasses\Test;

class YourClass {
    
    private $test;

    // Dependency injector will auto load "Test" class and the "Test" classes and so on.
    function __construct(Test $test) {
        $this->test = $test;
    }

    function start() {
        return $this->test->get("This is the start page");
    }
    
}

use MaplePHP\Container\EventHandler;

$logger = new EventHandler();
$logger->addHandler(new Logger());
$logger->addEvent(function() {
    var_dump("Executed in conjunction with logger every method");
    // You could add a mail function that will send log message to you,
});
echo $logger->error("A error message to log");
// Will log message AND execute the event

$logger = new EventHandler();
$logger->addHandler(new Logger(), ["emergency", "alert", "critical"]);
$logger->addEvent(function() {
    var_dump("Executed in conjunction with logger event method");
    // You could add a mail function that will send log message to you,
});
echo $logger->error("A error message to log");
// Will only log message
echo $logger->alert("A error message to log");
// Will log message AND execute the event

use MaplePHP\Container\Interfaces\EventInterface;

$callableFunction = new class implements EventInterface {

    public function someMethod(string $what): string
    {
        return "Hello {$what}!";
    }

    // Resolve method will be executed in conjunction 
    // with logger event method
    public function resolve(): void
    {
        var_dump($this->someMethod("world"));
    }
};

$logger = new EventHandler();
$logger->addHandler(new Logger(), ["emergency", "alert", "critical"]);
$logger->addEvent(function() {
    var_dump("Executed in conjunction with logger event method");
    // You could add a mail function that will send log message to you,
});
echo $logger->error("A error message to log");
// Will only log message
echo $logger->alert("A error message to log");
// Will log message AND execute the event