我試圖延長PHP5的MySQLi圖書館mysqli擴展和本地錯誤處理
class mysqli_ls extends mysqli
我擴展類的原因是爲了增加功能和特性我需要不包含在默認情況下。對於我創建的函數,我想以完全相同的方式處理錯誤,就像我對任何本機函數一樣。
我想使用該類的人不必確定函數是本地函數還是定製函數。
正常功能
如果($ _mysqli->查詢($ SQL)===假)模具($ _ mysqli->錯誤);
必需定製功能示例
如果($ _mysqli-> run_config()===假)管芯($ _ mysqli->錯誤);
類
在被延長的mysqli我的課mysqli_ls,我有我希望將設置父的mysqli類中的錯誤信息如下功能..
public function run_config()
{
// Open Debug FH
if ($this->config['debug_log'] === TRUE)
{
if ($this->fh_debug = fopen($this->config['debug_log_path'], 'a') === FALSE)
{
$this->handle_error('Could not open Debug file handle','200',__METHOD__);
return $this->E_ERROR;
}
}
}
private function handle_error($errormsg,$errorno,$method)
{
$this->errorno = $errorno;
$this->error = $errormsg;
if ($this->config['debug_log'] === TRUE)
{
$msg = "($errorno) $errormsg";
$this->write_log('error', $method, $msg);
}
return $this->E_OK;
}
我曾嘗試上述方法出錯: PHP致命錯誤:mysqli_ls :: handle_error():無法寫屬性
和靜態調用語法導致如下圖所示語法錯誤:
parent::error
parent::errorno
我想這個類的用戶有默認的mysqli功能和擴展功能無縫地,而不必使用不同的方法來識別錯誤。
感謝您的任何意見或給定的輸入..
編輯:添加完整的類
class mysqli_ls extends mysqli
{
private $config = array
(
'config_load' => FALSE,
'config_path' => 'mysqli_ls.database.ini',
'debug_log' => FALSE,
'debug_log_path' => '/tmp/mysqli_ls.debug.log',
'query_log' => FALSE,
'query_log_path' => '/tmp/mysqli_ls.debug.log',
'log_date_format' => 'Y-m-d H:i:s'
);
// Expected fields for the autoload file
private $db_fields = array('hostname','username','password','database','port');
// File Handles
private $fh_debug = FALSE;
private $fh_query = FALSE;
private $fh_config = FALSE;
// Return presets
private $E_OK = TRUE;
private $E_ERROR = FALSE;
// Existing database connections
private $db_existing = array();
public $error;
public $errorno;
## ----------
public function __construct()
{
if (!function_exists('mysqli_connect')) $this->handle_error('MySQLI is not installed','100');
}
## ----------
public function run_config()
{
// Testing error handling
$this->handle_error('Could not open Debug file handle','200',__METHOD__);
// Open Debug FH
if ($this->config['debug_log'] === TRUE)
{
if ($this->fh_debug = fopen($this->config['debug_log_path'], 'a') === FALSE)
{
$this->handle_error('Could not open Debug file handle','200',__METHOD__);
return $this->E_ERROR;
}
}
// Open Query FH
if ($this->config['query_log'] === TRUE)
{
if ($this->fh_query = fopen($this->config['query_log_path'], 'a') === FALSE)
{
$this->handle_error('Could not open Debug file handle','210');
return $this->E_ERROR;
}
}
// Load database INI file
if ($this->config['config_load'] === TRUE)
{
if (($this->db_existing = parse_ini_file($this->config['config_path'], TRUE)) === FALSE)
{
$this->handle_error('Could not parse the database ini file','220');
return $this->E_ERROR;
}
}
// Check for format of the loaded ini file
foreach($this->db_existing as $name => $row)
{
foreach($this->db_fields as $field)
{
if (!isset($row[$field]))
{
$this->handle_error("Missing field ($field) in the config array for element ($name)",'230');
return $this->E_ERROR;
}
} // END foreach
} // END foreach
return $this->E_OK;
}
## ----------
public function set_config($key, $value)
{
if (!isset($this->config[$key]))
{
$this->handle_error('Configuration variable ($key) does not exist','300');
return $this->E_ERROR;
}
$this->config[$key] = $value;
return $this->E_OK;
}
## ----------
public function get_config()
{
return array_merge($this->config, $this->db_existing);
}
## ----------
private function write_log($type,$method,$msg)
{
$msg = date($this->config['log_date_format']) ."\t". $type ."\t". $method ."\t". $msg ."\n";
switch($type)
{
case 'error':
fwrite($this->fh_debug, $msg);
break;
case 'debug':
fwrite($this->fh_debug, $msg);
break;
case 'query':
fwrite($this->fh_query, $msg);
break;
default:
return $this->E_ERROR;
break;
}
}
## ----------
private function handle_error($errormsg,$errorno,$method)
{
$this->errorno = $errorno;
$this->error = $errormsg;
if ($this->config['debug_log'] === TRUE)
{
$msg = "($errorno) $errormsg";
$this->write_log('error', $method, $msg);
}
return $this->E_OK;
}
## ----------
## ----------
## ----------
## ----------
} // END Class
測試腳本調用類
#!/usr/bin/php
<?php
require('mysqli_ls.class.php');
try
{
$_mysqli = new mysqli_ls;
}
catch (Exception $e)
{
print "\n\n". $e->getMessage() ."\n\n";
print_r($e);
}
$_mysqli->set_config('debug_log',TRUE);
$_mysqli->set_config('query_log',TRUE);
$_mysqli->set_config('config_load',TRUE);
$_mysqli->run_config();
print_r($_mysqli->get_config());
?>
編輯:所以它看起來像MySQLi類中的錯誤變量可能是隻讀的。是否有解決此問題的另一種方法?我想可能有我自己的錯誤函數,檢查父類和我自己的變量。
對不起有兩個..生病正確的,上面的代碼 – Lee
這是一個直接複製粘貼?在你的例子中,你沒有在定義handle_error之前關閉run_config函數。 –
不,不是一個直接的複製和粘貼..我已經刪除了大量的中間膨脹。腳本運行語法明智,它更多的是我有一個概念問題..感謝指出缺少捲曲出來,但在主要職位糾正。 – Lee