不知道更好的标题,但这是我的代码.
我有类用户在实例化时检查表单数据,但我得到以下错误/通知:
Notice: Use of undefined constant username - assumed 'username' in C:\Users\Jinxed\Desktop\WebTrgovina\app\m\Register\User.m.php on line 7
Notice: Use of undefined constant password - assumed 'password' in C:\Users\Jinxed\Desktop\WebTrgovina\app\m\Register\User.m.php on line 7
Notice: Use of undefined constant passwordc - assumed 'passwordc' in C:\Users\Jinxed\Desktop\WebTrgovina\app\m\Register\User.m.php on line 7
... and so on for every defined variable in user class.
Run Code Online (Sandbox Code Playgroud)
这是用户类:
class User {
function __construct(){
$test = 'blah';
$username; $password; $passwordc; $name; $surname; $address;
$this->checkInput(array(username=>20, password=>20, passwordc=>20, name=>20, surname=>40, address=>40));
}
//array(formName=>CharacterLimit)
private function checkInput($fields){
foreach($fields as $field=>$limit){
if($_POST[$field]=='' || strlen($_POST[$field])>$limit) $this->error[] = "$field must be filled in, and it must be less than or $limit characters long.";
else $this->{$field} = $_POST[$field];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不太明白是什么问题,我首先尝试创建变量,而不是从调用checkInput方法的主程序,但我得到相同的错误.
您应该引用用作数组键的字符串:
$this->checkInput(array(
'username'=>20,
'password'=>20,
'passwordc'=>20,
'name'=>20,
'surname'=>40,
'address'=>40));
Run Code Online (Sandbox Code Playgroud)
该文件说:
如果使用未定义的常量,PHP假定您指的是常量本身的名称,就像您将其称为字符串(
CONSTANTvs"CONSTANT")一样.E_NOTICE发生这种情况时,将发出级别错误.另请参阅手册输入,了解为什么$foo[bar]错误(除非您首先define()bar作为常量).
| 归档时间: |
|
| 查看次数: |
5896 次 |
| 最近记录: |