Dependency injection¶
DALMP offers a set of classes that allows you to work with MySQL, Cache backends and sessions, but when trying to use all this together, some perfomance issues may be raised, so to deal with this, a DI (dependecy injector) is high recomendable.
The idea of using a DI is to load once, and use any time with out need to reload the objects.
abstractDI¶
abstractDI is the name of an abstrac class that works as a base for building a dependecy injector.
Note
The abstracDI class can be used as a base for any project not only DALMP
DI¶
DI (Dependecy Injector)
extends abstractDI
and creates the DI for DALMP.
Example¶
Using mysql, cache (redis), sessions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php
require_once 'dalmp.php';
$di = new DALMP\DI();
$user = getenv('MYSQL_USER') ?: 'root';
$password = getenv('MYSQL_PASS') ?: '';
$host = getenv('MYSQL_HOST') ?: '127.0.0.1';
$port = getenv('MYSQL_PORT') ?: '3306';
$DSN = "utf8://$user:$password".'@127.0.0.1/test';
$db = $di->database($DSN);
$cache = $di->cache($redis_cache);
$sessions = $di->sessions($di->sessions_redis($redis_cache), 'sha512');
$sessions->regenerate_id(true);
$db->useCache($cache);
$now = $db->CachegetOne('SELECT NOW()');
echo $now, PHP_EOL;
echo session_id();
|
TODO Examples
Thanks Navicat for supporting Open Source projects.
A great amount of time has been spent creating, crafting and maintaining this software, please consider donating.
Donating helps ensure continued support, development and availability.
comments powered by Disqus