1.代码示例

<?php
namespace extend\drive;

use \PDO;
use \Exception;
use PDOException;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-10-5
 * Time: 上午10:09
 */

class Db{

    protected static $_instance = null;
    protected $dbName = '';
    protected $dsn;
    protected $dbn;

    /**
     * 构造方法
     * @author mmy
     */
    public function __construct($dbHost,$dbUser,$dbPasswd,$dbName,$charSet)
    {
        try
        {
            $this->dsn = 'mysql:host='.$dbHost.';dbname='.$dbName;
            $this->dbn = new PDO($this->dsn, $dbUser, $dbPasswd);
            $this->dbn->exec('SET character_set_connection='.$charSet.', character_set_results='.$charSet.'character_set_client=binary');
        }catch(PDOException $e){
           $this->outputError($e->getMessage());
        }
    }

    /**
     * 输出错误信息
     * @author mmy
     */
    private  function outputError($errorMessage)
    {
        throw new Exception('MySql Error:'.$errorMessage);
    }

    /**
     * 单例模式
     * @author mmy
     * @ return obj
     */
    public static function getInstance($dbHost,$dbUser,$dbPasswd,$dbName,$charSet)
    {
        if(self::$_instance===null)
        {
            self::$_instance = new self($dbHost,$dbUser,$dbPasswd,$dbName,$charSet);
        }
        return self::$_instance;
    }

    /**
     * 防止克隆
     * @author mmy
     */
    private function __clone(){}

    /**
     * 调试方法
     * @author mmy
     */
    private function debug($debugInfo)
    {
        var_dump($debugInfo);
        exit();
    }

    /**
     * 执行sql语句
     * @author mmy
     *
     */
    public function execSql($sql, $debug=false)
    {
        if($debug===true)
        {
            $this->debug($sql);
        }
        $result = $this->dbn->exec($sql);
        $this->getPDOError();
        return $result;
    }

    /**
     * 捕获pdo错误消息
     * @author mmy
     */
    private function getPDOError()
    {
        if($this->dbn->errorCode() !='00000')
        {
            $arrayError = $this->dbn->errorInfo();
            $this->outputError($arrayError[2]);
        }
    }
}

循环引用目录当中的文件

// 加载所有Applications/*/start.php,以便启动所有服务
foreach(glob(__DIR__.'/Applications/*/start*.php') as $start_file)
{
    require_once $start_file;
}

mmy123456
376 声望17 粉丝

有项目请联系:15201970281(毛毛)