<?php
define('DIR',__DIR__);
include DIR .'/Common/Loder.php';
spl_autoload_register('\Common\Loder::autoLoder'); //自动加载类
$database = \Common\Factory::createDatabase('************','root','','memcached_test'); //数据库连接
$memcache = \Common\Factory::addServers('*****',11211); // memcache 服务器地址 端口 如果在这里直接new memcache 是可以的
/**************/
<?php
namespace Common;
class Factory
{
static function createDatabase($localhost, $user, $pwd, $datebase)
{
$db = Register::get($datebase); //从注册器中取出对象
if (!$db) {
$db = new Datebase($localhost, $user, $pwd, $datebase);
Register::set($datebase, $db); //向注册器中注册Datebase
}
return $db;
}
static function addServers($localhost, $port)
{
$key = $localhost.'_me';
$db = Register::get($key); //从注册器中取出对象
if (!$db) {
$db = new Me($localhost,$port);
Register::set($key, $db); //向注册器中注册Me
}
return $db;
}
}
/***************************/
<?php
/**
- Created by PhpStorm.
- User: Administrator
- Date: 2016/7/27
- Time: 18:39 */
namespace Common;
class Me
{
function __construct($localhost,$port)
{
$m = new Memcache(); // 进入这里 浏览器就会报一个 500 的错误
$m->addServer($localhost,$port);
return $m;
}
}