Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 调用类中的关联数组_Php_Arrays - Fatal编程技术网

Php 调用类中的关联数组

Php 调用类中的关联数组,php,arrays,Php,Arrays,我很确定有一个简单的解决方案,我没有看到,但目前正在尝试创建一个简单的系统,在允许用户继续之前检查用户的用户名和密码输入。如果检查不起作用,将显示一条错误消息,指示其输入有何错误。我试图通过一个关联数组来实现这一点,如下所示: public function check() { if (strlen($this->_username) < $this->_minimumChars) { $this->_error['idlength'] = "Username

我很确定有一个简单的解决方案,我没有看到,但目前正在尝试创建一个简单的系统,在允许用户继续之前检查用户的用户名和密码输入。如果检查不起作用,将显示一条错误消息,指示其输入有何错误。我试图通过一个关联数组来实现这一点,如下所示:

  public function check() {
if (strlen($this->_username) < $this->_minimumChars) {
    $this->_error['idlength'] = "Username needs to be longer than $this->_minimumChars characters";
}
if (!preg_match('/^[a-zA-Z0-9]+$/', $this->_username)) {
    $this->_error['idformat'] = "Username can only contain letters and numbers";
}
if (strlen($this->_password) < $this->_minimumChars) {
    $this->_error['pwdlength'] = "Password must be longer than $this->_minimumChars characters";
}
if (empty($this->_error)) {
    return $this->_result = true;
} }
通过使用$errors=$checker->getError;其中,$checker是该类的一个实例。 但是,当我尝试使用$errors数组时,似乎什么都没有启动。我曾尝试通过在条件语句中使用空$errors['idlength']对其进行故障排除,但数组中似乎没有元素


有人能告诉我哪里出错了吗?

你确定用户名和密码都设置好了吗?您确定正在调用check函数吗?你确定用户名和密码没有通过检查吗?我为用户名/密码通过检查创建了一个成功重定向,当我输入通过检查的用户名/密码时,它会正确执行相应的命令。只有当用户名/密码未通过检查时,$errors数组才能正常工作。您可以尝试在构造函数中初始化$\u error属性,而不是像那样的内联属性,这可能无关紧要。我会更改此行,返回$this->\u result=true;到$this->_result=true;返回$this->\u结果;看看会发生什么。。没关系,找到了解决办法。。在调用check之前调用了getError方法。谢谢你!
  public function getError() {
return $this->_error; }