4

一連のプロパティ (構成) を新しく作成されたインスタンスに適用する最も効率的な方法を探しています。私の最初の目的は、アプリケーションをオブジェクト指向に保つことです。2 つ目の目的は、DI コンテナーを操作できるようにすることです。これは私がこれまでに思いついたサンプルです:

class ViewLogin {
  public $msgLoginGranted;
  public $msgLoginFailed;

  public function __construct(){
  }

  protected function onSuccess() {
    return $this->msgLoginGranted;
  }

  protected function onFailure() {
    return $this->msgLoginFailed;
  }
}

class ControllerLogin {
  public function __construct(ModelLogin $model, ViewLogin $view) {
  }
}

ViewLogin をきれいに保ち、構成データをコードから分離するには、次のことを行うのが最善です。

新しいクラス ViewLogin1 を作成します

class ViewLogin1 extends ViewLogin {
  public function __construct() {
    $this->msgLoginGranted = 'Welcome!';
    $this->msgLoginFailed = 'Login Failed!';
  }
}

短所: 静的なクラス コンテンツ、新しい機能がない、クラス スペースを汚染する

構成オブジェクトを ViewLogin に渡す

class ViewLogin {
  public function __construct(Config1 $config) {
    $this->msgLoginGranted = $config->msgLoginGranted;
    $this->msgLoginFailed = $config->msgLoginFailed;
  }
}

ViewLogin のデコレータを作成しますか?

構成を XML/JSON/YAML に移動...

4

1 に答える 1