1

我有一个类,它有一些关于帐户管理的功能,我还有一个验证电子邮件地址、用户名等的类。

如何在帐户类中使用验证类?我如何轻松地包含它?

4

3 回答 3

1

您可以实例化验证类并将其作为参数传递给帐户类,并将其设置为帐户构造方法中帐户类中的属性吗?

于 2013-01-09T15:01:37.077 回答
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;
}
于 2013-01-09T15:03:39.907 回答
-1

您可以使用方法创建第三类“AccountsManagementValidator”

validate(AccountManagement): void
hasErrors(): boolean
getErrors(): List<Error>

有很多不同的实现,但我描述的是 PlayFramework 验证器实现。

当然,您的“验证器”类会重用您的常用方法来验证电子邮件、用户等......

于 2013-01-09T15:06:18.640 回答