[英]PHP Fatal error: Cannot redeclare function
I am trying to write a logging function and so far it works. 我正在尝试编写日志记录功能,到目前为止它的工作原理。 The only issue I am facing right now is that I can execute the function only one time successfully.
我现在面临的唯一问题是我只能成功执行一次该功能。 When I call it a second time PHP states
PHP Fatal error: Cannot redeclare function
. 当我第二次调用它时PHP表示
PHP Fatal error: Cannot redeclare function
。
Now I wanted to ask: 现在我想问:
Why does PHP complain about my workflow? 为什么PHP会抱怨我的工作流程? To my understanding I write the function placeholders3 and I can use this function again and again with different parameters.
据我所知,我编写了函数占位符3,我可以使用不同的参数一次又一次地使用这个函数。 E. g.
E. g。 first:
第一:
$addNewCar->log(array('makeModelDescription','firstRegistration','mileage','logMessage', 'createTime'), array($carDetails[0]['FK_makeModelDescription'], $carDetails[0]['FK_firstRegistration'], $carDetails[0]['FK_mileage'], 'deleteCarbyID: ' . $addNewCar->affected_rows, time() )
); ); and then eg
然后例如
$addNewCar->log(array('makeModelDescription','firstRegistration','mileage','logMessage', 'createTime'), array($carDetails[0]['FK_makeModelDescription'], $carDetails[0]['FK_firstRegistration'], $carDetails[0]['FK_mileage'], 'insertCarById: ' . $addNewCar->affected_rows, time() ));
Function logging: 功能记录:
public function log($key, $values) {
try {
/** @var $tableColumns Column names in MySQL table */
$tableColumns = $key;
/** @var $tableRows Use 2-dimensional numeric or associative arrays
* for multiple records. Single records can be added with a
* vector */
$tableRows[] = $values;
$DBH = $this->DBH;
/**
* @param $text Unnamed placeholder
* @param int $count Number of unnamed placeholders (size of one array)
* @param string $separator Separator
*
* @return string Concatenated unnamed placeholders, e. g. ?, ?, ?
*/
function placeholders3($text, $count=0, $separator=","){
$result = array();
if($count > 0){
for($x=0; $x<$count; $x++){
$result[] = $text;
}
}
return implode($separator, $result);
}
/**
* Generate unnamed placeholders for SQL statement,
* e. g. (?,?,?,?,?),(?,?,?,?,?),(?,?,?,?,?).
*
* Pitfalls:
*
* - Nested array: Take care that you iterate over the items
* of the nested array. E. g. $tableRows[0]
* instead of $tableRows;
*
* - sizeof($d): Number of elements per single array
* (for a single db insert).
*/
$insert_values = array();
foreach($tableRows as $d){
$question_marks[] = '(' . placeholders3('?', sizeof($d)) . ')';
$insert_values = array_merge($insert_values, array_values($d));
}
$sql = "INSERT INTO logging (" . implode(",", $tableColumns ) . ") VALUES " . implode(',', $question_marks);
$STH = $DBH->prepare($sql);
$STH->execute($insert_values);
$this->affected_rows = $STH->rowCount();
}
catch(PDOException $e) {
error_log("PDO error: " . $e->getMessage());
}
}
Many thanks in advance! 提前谢谢了!
Functions are declared in the global scope. 函数在全局范围内声明。 You are re-declaring the function in the global scope every time you call the log() method.
每次调用log()方法时,都会在全局范围内重新声明该函数。 There is an easy solution:
有一个简单的解决方案:
if (!function_exists('placeholders3')) {
function placeholders3($text, $count=0, $separator=","){
// ...
}
}
But you are still declaring a global function, which you probably don't want to do. 但是你仍然在声明一个全局函数,你可能不想这样做。 Since you are using an object anyway, the correct way of doing this is:
既然你正在使用一个对象,那么这样做的正确方法是:
class myclass {
protected function placeholders3($text, $count=0, $separator=","){
//...
}
public function log($key, $values) {
// ...
$question_marks[] = '(' . $this->placeholders3('?', sizeof($d)) . ')';
// ...
}
}
If for some reason you don't want to create the class method and you are using a relatively recent version of PHP, you can also use a lambda function: 如果由于某种原因您不想创建类方法并且您使用的是相对较新版本的PHP,则还可以使用lambda函数:
public function log($key, $values) {
$placeholders3 = function($text, $count=0, $separator=","){
// ...
};
// ...
$question_marks[] = '(' . $placeholders3('?', sizeof($d)) . ')';
// ...
}
还有伙计们,请检查你的函数名是否是PHP中的关键字,比如'reset' - 在这种情况下,它还会显示“无法重新声明函数”错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.