gpt4 book ai didi

php - MySQLI 扩展和原生错误处理

转载 作者:行者123 更新时间:2023-12-03 08:35:55 24 4
gpt4 key购买 nike

我正在尝试扩展 PHP5 MySQLi 库

class mysqli_ls extends mysqli

我扩展该类的原因是添加默认情况下不包含的我需要的功能和特性。对于我创建的函数,我想以与处理任何 native 函数完全相同的方式处理错误。

我希望使用该类的人不必确定这些功能是原生的还是定制的。

正常功能

if ($_mysqli->query($sql) === false) die($_mysqli->error);

所需的定制功能示例

if ($_mysqli->run_config() === false) 死($_mysqli->error);

类(class)

在我的扩展 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 fatal error :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 类中的错误变量可能是只读的。还有其他方法可以解决这个问题吗?我想也许有我自己的错误函数来检查父类和我自己的变量。

最佳答案

使用 __get魔术函数,您可以做一些检查是否设置了父级的错误变量。像这样的东西:

class mysqli_ls extends mysqli{
public $error;
public $errorno;
public $connect_error;
public $connect_errorno;

function __get($key){
if(in_array($key, array('error', 'errorno', 'connect_error', 'connect_errorno'))){
$parentValue = parent::$key;
if(empty($parentValue)){
return $this->$key;
}
return $parentValue;
}
}
}

小测试脚本:
<?php
$test = @new mysqli2('localhost', 'badlogin', 'whatisapassword', 'anonexistentdb');
echo 'test : '.$test->connect_error;

关于php - MySQLI 扩展和原生错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6749175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com