我有一个关于帐户管理功能的课程,我还有一个验证电子邮件地址,用户名等的课程。
如何在帐户类中使用验证类?我如何轻松地将它包括在内?
答案 0 :(得分:1)
您可以实例化验证类并将其作为参数传递到accounts类中,并将其设置为accounts构造函数方法中accounts类中的属性吗?
答案 1 :(得分:1)
如果一个类专门用于验证目的而另一个类旨在包含特定于实例的信息,那么您可以采用三种不同的方法。
1)静态引用验证器(推荐):
<?php
class Validation {
public static function validateEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
}
class AccountManagement {
public $email;
public function __construct($email) {
$this->email = $email;
// Validate the e-mail. If not valid, an exception is thrown.
if(!Validation::validateEmail($this->email)) {
throw new InvalidArgumentException('$email argument supplied must contain a valid e-mail address');
}
}
}
2)扩展您的实例类以继承验证类
<?php
class Validation {
public function validateEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
}
class AccountManagement extends Validation {
public $email;
public function __construct($email) {
$this->email = $email;
// Validate the e-mail. If not valid, an exception is thrown.
if(!$this->validateEmail($this->email)) {
throw new InvalidArgumentException('$email argument supplied must contain a valid e-mail address');
}
}
}
3)在您的实例类中实例化您的验证类(不推荐):
<?php
class Validation {
public function validateEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
}
}
class AccountManagement extends Validation {
public $email;
public function __construct($email) {
$this->email = $email;
$validator = new Validation;
// Validate the e-mail. If not valid, an exception is thrown.
if(!$validator->validateEmail($this->email)) {
throw new InvalidArgumentException('$email argument supplied must contain a valid e-mail address');
}
}
}
测试您的实施
一旦您选择了最适合您需求的方法(无论您选择哪种方法),您都可以使用以下代码进行测试:
// Valid, should not throw an exception and should print success.
try {
$account = New AccountManagement('me@myself.com');
print "AccountManagement object successfully instantiated.<br />\r\n";
} catch(Exception $e) {
print 'Error: Encountered ' . $e;
}
// Invalid, should throw an InvalidArgumentException exception
try {
$account = New AccountManagement('myself.com');
print "AccountManagement object successfully instantiated.<br />\r\n";
} catch(Exception $e) {
print 'Error: Encountered ' . $e;
}
奖金示例:
有时,您可能希望专门捕获验证错误,并让某些事情处理可能遇到的任何其他异常。在这种情况下,我们可以为我们的验证器创建一个特殊的异常,并允许验证器成为抛出异常的那个:
<?php
class ValidationException extends Exception {
// Will use default Exception behavior.
}
class Validation {
public static function validateEmail($email) {
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new ValidationException('E-mail address supplied is not valid');
}
}
}
class AccountManagement {
public $email;
public function __construct($email) {
$this->email = $email;
// Validate the e-mail.
Validation::validateEmail($this->email);
}
}
// Valid, should not throw an exception and should print success.
try {
$account = New AccountManagement('me@myself.com');
print "AccountManagement object successfully instantiated.<br />\r\n";
} catch(Exception $e) {
print 'Error: Encountered ' . $e;
}
// Invalid, should throw an ValidationException exception
try {
$account = New AccountManagement('myself.com');
print "AccountManagement object successfully instantiated.<br />\r\n";
} catch(ValidationException $e) {
print 'Error: Encountered ' . $e;
}
答案 2 :(得分:-1)
您可以创建第三类&#34; AccountsManagementValidator&#34;方法
validate(AccountManagement): void
hasErrors(): boolean
getErrors(): List<Error>
有很多不同的实现,但我描述的是PlayFramework验证器实现。
当然你的验证员&#34;类重用您的常用方法来验证电子邮件,用户等...