From 28bc991048c22becb1915029ae8f8e7a38202584 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sun, 30 Apr 2023 22:50:07 +0900 Subject: [PATCH] Replace some rarely used DB class properties with __get() magic method --- common/framework/DB.php | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/common/framework/DB.php b/common/framework/DB.php index 1dae7ac3c..6b3d17d15 100644 --- a/common/framework/DB.php +++ b/common/framework/DB.php @@ -22,6 +22,7 @@ class DB */ protected $_type = ''; protected $_prefix = ''; + protected $_version = ''; protected $_charset = 'utf8mb4'; protected $_engine = 'innodb'; @@ -51,13 +52,6 @@ class DB */ protected $_transaction_level = 0; - /** - * Properties for backward compatibility. - */ - public $db_type = 'mysql'; - public $db_version = ''; - public $use_prepared_statements = true; - /** * Get a singleton instance of the DB class. * @@ -129,9 +123,6 @@ class DB throw new Exceptions\DBError($e->getMessage()); } - // Get the DB version. - $this->db_version = $this->_handle->getAttribute(\PDO::ATTR_SERVER_VERSION); - // Cache the debug comment setting. $this->_debug_queries = in_array('queries', Config::get('debug.display_content') ?: []); $this->_debug_comment = !!config('debug.query_comment'); @@ -1271,6 +1262,24 @@ class DB $this->_debug_comment = $enabled; } + /** + * Magic method to support some read-only properties for backward compatibility. + * + * @param string $key + * @return mixed + */ + public function __get($key) + { + switch ($key) + { + case 'db_type': return $this->_handle->getAttribute(\PDO::ATTR_DRIVER_NAME); + case 'db_version': return $this->_handle->getAttribute(\PDO::ATTR_SERVER_VERSION); + case 'prefix': return $this->_prefix; + case 'use_prepared_statements': return true; + default: return null; + } + } + /** * ========================== DEPRECATED METHODS ========================== * ==================== KEPT FOR COMPATIBILITY WITH XE ====================