我有一个类在我的Web应用程序中注册用户,构造函数进行基本的验证和清理。问题是,当我尝试转储值时,它们都是空字符串。以下是RegisterUser
类中的代码:
<?php
class RegisterUser {
public $firstName = "";
public $lastName = "";
public $email = "";
public $password = "";
// Validating registration data
function _construct($firstName, $lastName, $email, $password) {
$firstName = filter_var($firstName, FILTER_SANITIZE_STRING);
$lastName = filter_var($lastName, FILTER_SANITIZE_STRING);
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$password = filter_var($password, FILTER_SANITIZE_STRING);
if (empty($firstName) || !preg_match("^[A-Za-z.\s_-]+$", $firstName)) {
throw new Exception("First name not filled in or is invalid.");
}
if (empty($lastName) || !preg_match("^[A-Za-z.\s_-]+$", $lastName)) {
throw new Exception("Last name not filled in or is invalid.");
}
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new Exception("Email not filled in or is invalid.");
}
if (empty($password) || !preg_match("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*]).{8,}", $password)) {
throw new Exception("Password not filled in or is invalid.");
}
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
$this->password = $password;
}
}
这是我在index.php
文件中的调用方式:
$register = new RegisterUser($registerFormData["firstName"], $registerFormData["lastName"], $registerFormData["email"], $registerFormData["password"]);<br>
我还完成了var_dump($registerFormData)
,它返回了正确提交的值。
这是简短的版本:
为什么当我尝试var_dump($register)
时,我会得到所有空白值吗?
答案 0 :(得分:4)
您必须将两个下划线与构造函数一起使用,如下所示:
function __construct($firstName, $lastName, $email, $password) {
有关构造函数here
的更多信息