1

* php オブジェクト指向コーディングでこのタイプの問題を処理する方法に関する問題/混乱 *

顧客のサービスを一時停止する必要がある顧客クラスがありますが、顧客がサービスの保留中の作業タイプを持っている場合、呼び出し元の関数がエラー処理を行うために false を返す必要があります (ここではできません。電子メール、出力、または html)

ただし、次のコードを使用するかのようにこれを処理する方法が混乱しています foreach ループの最後の条件でのみ false を返します

 /**
   * return false on failier 
   * Customer suspend all services for this customer
   * 
   */
  public function suspendServices(){

    $pending=false; 

    foreach ($this->services() as $service) {

    $pending = $service->hasPendingWorktypes();

    if($pending === true) {
        return false;
    }   
    $service->state()->changeTo(8);  

    }//end of foreach services 

  }//end of function
4

1 に答える 1

0

例外はこの仕事に最適です。サービスを呼び出すビジネス メソッドは、適切な結果を得るために効率的にパーツを処理できます。

public function suspendServices(){

    $pending=false; 

    foreach ($this->services() as $service) {

    $pending = $service->hasPendingWorktypes();

    if($pending === true) {
        throw new PendingExcpetion(); //Throw the exception
    }   
    $service->state()->changeTo(8);  

    }//end of foreach services 
}
于 2012-06-13T10:12:55.607 に答える