Replace some rarely used DB class properties with __get() magic method

This commit is contained in:
Kijin Sung 2023-04-30 22:50:07 +09:00
parent 749037249c
commit 28bc991048

View file

@ -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 ====================