我想使用 FILTER_VALIDADE_EMAIL 但它给了我一个警告说它需要一个字符串。我是 php 新手,我在尝试使用 OO 时遇到了麻烦。
<?php
Class User {
private $id;
private $login_name;
private $hashed_password;
private $email;
public function __construct($login_name, $email){
$this->login_name = $login_name;
$this->email = $email;
}
public function getLoginName(){
return $this->login_name;
}
public function setLoginName(String $login_name){
$this->login_name = $login_name;
}
public function setEmail(String $email){
$this->email = $email;
}
public function getEmail(String $email){
return (string) $this->email;
}
}?>
<?php
include("../lib/php/User.php");
class UserDAO {
private $user;
public function __construct(){
}
public function getUser(){
return $this->user;
}
public function setUser(User $user){
$this->user = $user;
}
public function insertUser(User $user){
$email = $user->getEmail();
$login_name = $user->getLoginName();
//ver http://www.php.net/manual/en/function.filter-var.php
if(filter_var($email, FILTER_VALIDATE_EMAIL) && empty($login_name)){
echo "valid user";
}
}
}
$user = new User("user","user@gmail.com");
$userDAO = new UserDAO();
$userDAO->insertUser($user); ?>
返回的错误是
PHP Catchable 致命错误:传递给 User::getEmail() 的参数 1 必须是 String 的实例,没有给出