Можно самый простой на мой взгляд.
<?php
class Container {
protected static $storage = [];
public static function set ($name, $data) {
self::$storage[$name] = $data;
}
public static function get ($name) {
return isset(self::$storage[$name]) ? self::$storage[$name] : false;
}
}
$config = [
'host' => 'localhost',
'dbname' => 'base',
'user' => 'root',
'password' => '',
'type' => 'mysql',
'charset' => 'utf8'
];
$DB = new \PDO($config['type'] . ":dbname=" . $config['dbname'] . ";host=" . $config['host'], $config['user'], $config['password'], []);
$DB->setAttribute(\PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES " . $config['charset'] . "; SET TIME_ZONE = '" . date_default_timezone_get() . "'");
$DB->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$DB->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
Container::set('DB', $DB);
$MemCache = new Memcache;
$MemCache->addServer('127.0.0.1', 11211);
Container::set('MemCache', $MemCache);
function xxx () {
$DB = Container::get('DB');
$data = $DB->query('...')->fetchAll();
$result = [];
foreach ($data as $item) {
$result[] = $item['id'];
}
return $result;
}
function yyy ($key) {
$MemCache = Container::get('MemCache');
return $MemCache->get($key);
}
print_r(xxx());
echo yyy('test');
_____________
Промокод предоставляет скидку на заказ домена и/или хостинга reg.ru
BFCC-3895-8804-9ED2