Skip to content

Instantly share code, notes, and snippets.

@y2468101216
Created March 6, 2019 06:52
Show Gist options
  • Save y2468101216/27f36be408288e2a657597ff3cfe4f3f to your computer and use it in GitHub Desktop.
Save y2468101216/27f36be408288e2a657597ff3cfe4f3f to your computer and use it in GitHub Desktop.
abstract class InvoiceValidation
{
  protected function phoneValidate() 
  {
      ...
  }
  
  //執行驗證
  abstract public function run();
}
class EmailInvoiceValidation extends InvoiceValidation
{
  protected function emailValidate() 
  {
     ....
  }
  
  //執行驗證
  public function run()
  {
     ....
  }
}
class RealMailInvoiceValidation extends InvoiceValidation
{
  protected function addressValidate() 
  {
     ....
  }
  
  //執行驗證
  public function run()
  {
     ....
  }
}

第一種方法

$className = $method.'InvoiceValidation';

if (!class_exists($className)) {
    return null;
}

$validate = new $className($item->member_id, $method, $item->amount);

$message = $validate->run();

第二種方法

 switch ($method) {
    case "RealMail":
        $validate = new RealMailInvoiceValidation();
        break;
    case "Email":
        $validate = new EmailInvoiceValidation();
        break;
    default:
        return null;
}

$message = $validate->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment