我已经升级到PHP 5.4,现在我收到了这个错误消息,我知道为什么会收到这个消息,但需要找出如何修复它。我知道这是因为当它扩展父类validate()方法时,我在子类中有额外的参数。
严格标准:ValidateName::validate()的声明应为与Validator::validate($validateThis)兼容C: 第4行上的''www''testing''ValidateName.php
我在这里看到,有些人说要使用func_get_args(),但另一些人说不要使用它。
如果出现此错误,我该如何处理?
我的父类
//Constructor
public function validate($validateThis) {}
// Function to add all the error messages to the array
public function setError($msg) {
$this->errors[] = $msg;
}
// Function to check if the validation passes
public function isValid() {
if (count($this->errors) > 0) {
return false;
} else {
return true;
}
}
// Function to get each of the errors
public function fetch() {
$error = each($this->errors);
if ($error) {
return $error['value'];
} else {
reset($this->errors);
return false;
}
}
}
?>
和我的孩子班
class ValidateName extends Validator {
public function ValidateName ($name, $field) {
// Create an array of errors
$this->errors = array();
// Validate the text for that field
$this->validate($name, $field);
}
public function validate() {
// If any of the text fields are empty add an error message to the array
if(empty($name)) {
$this->setError($field.' field is empty');
}
else {
// Validate the text fields against the regex. If it fails add error message to array
if (!preg_match('/^[a-zA-Z- ]+$/', $name)) {
$this->setError($field.' contains invalid characters');
}
// if the length of the field is less than 2, add error message to array
if (strlen($name) < 2) {
$this->setError($field.' is too short');
}
// if the length of the field greater than 30, add error message to array
if (strlen($name) > 50) {
$this->setError($field.' is too long');
}
}
}
}
?>
// constructor
注释错误,由于类名为Validator
,请改进您的问题:-)- 您可以通过在子对象的
validate()
方法中添加$ignored
参数来"修复"它,但这可能是一个设计问题:您应该决定是将要验证的对象作为参数传递给validate
方法,还是在构造过程中(此选择会稍微改变类的性质) - 根据LSP
由于PHP 5.3.3
是的方法构造函数
__construct
所以你必须使用:
class ValidateName extends Validator {
public function __construct($name, $field) { //It must not be ValidateName
// Create an array of errors
$this->errors = array();
// Validate the text for that field
$this->validate($name, $field);
}
}
派生类的构造函数可能与父类的不同,但如果编写public function ClassName()
,它将被视为一种常用方法,并且在派生时,应该实现它必须接受的参数。