PHP

實例化pdo操作類的基礎模型類

字號+ 編輯: 种花家 修訂: 人在硅谷 來源: 原创 2023-09-10 我要說兩句(0)

這篇文章需要結合本站關於pdo操作類的文章一起看。

继上一篇ccORM里的pdo操作类的代码, 再写一点可实例化这个操作类的基础模型代码, 笔者在yaf框架项目上使用的, 支持手动选择读写和事务连接, 供大伙参考用。

注意: php版本需求 > 7.0

    class Pdo_Initiator
    {
        /**
         * 实例化容器
         * @var  array
         */
        protected static $_container = [];
        /**
         * 获取对象单例
         * @access  public
         * @return  self
         */
        public static function instance()
        {
            if (empty(static::$_container[static::class]) || array_key_exists(static::class, static::$_container) == false) {
                static::$_container[static::class] = new static();
            }
            return static::$_container[static::class];
        }
        /**
         * 初始化配置
         * @access  private static
         * @return  array
         */
        private static function _initConfig(): array
        {
            $config = Yaf\Registry::get('config');
            $db = $config->db;
            $redis = $config->redis;
            return [
                'host'       => $db->host,
                'port'       => $db->port,
                'user'       => $db->user,
                'password'   => $db->password,
                'charset'    => $db->charset,
                'dbname'     => $db->dbname,
                'prefix'     => $db->prefix,
                'persistent' => 0,
                'log'        => 1,
                'redis_ip'   => $redis->host,
                'redis_port' => $redis->port,
                'redis_auth' => $redis->auth,
                'redis_db'   => $redis->sql_cache,
                'redis_log'  => $redis->log,
            ];
        }
        /**
         * 获取读数据用句柄
         * @access  protected
         * @return  object
         */
        protected function on()
        {
            return new \Pdo_Worker(self::_initConfig());
        }
    }


閲完此文,您的感想如何?
  • 有用

    0

  • 沒用

    0

  • 開心

    0

  • 憤怒

    0

  • 可憐

    0

1.如文章侵犯了您的版權,請發郵件通知本站,該文章將在24小時内刪除;
2.本站標注原創的文章,轉發時煩請注明來源;
3.交流群: PHP+JS聊天群

相關課文
  • mac開發接入微信公衆號接口返回報錯 cURL error 56: SSLRead() return error -9806

  • pecl安裝程序時報錯Array and string offset access syntax with curly braces is no longer supported

  • PHP的換行符是什麽

  • 由於商家傳入的H5交易參數有誤,該筆交易暫時無法完成,請聯系商家解決

我要說說
網上賓友點評