<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ## 依賴注入 > 1、構造函數注入 為郵件服務定義抽象層 `EmailSenderInterface` ```php interface EmailSenderInterface { public function send(); } ``` 定義`Gmail`郵件服務 ```php class GmailSender implements EmailSenderInterface { // 實現發送郵件的類方法 public function send() { // TODO: Implement send() method. echo __CLASS__; } } ``` 定義`Tencent`郵件服務 ```php class TencentSender implements EmailSenderInterface { // 實現發送郵件的類方法 public function send() { // TODO: Implement send() method. echo __CLASS__; } } ``` 定義評論類 ```php class Comment { // 用于引用發送郵件的庫 private $_eMailSender; // 構造方法注入 public function __construct(EmailSenderInterface $emailSender) { $this->_eMailSender = $emailSender; } // 保存評論 public function save() { $this->afterInsert(); } // 當有新的評價,即 save() 方法被調用之后中,會觸發以下方法 protected function afterInsert() { $this->_eMailSender->send(); // 發送郵件 } } ``` 實例化兩種不同的郵件服務,當然,他們都實現了`EmailSenderInterface` ```php // 用構造函數將GmailSender注入 $comment1 = new Comment(new GmailSender()); // 使用Gmail發送郵件 $comment1->save(); // GmailSender // 用構造函數將MyEmailSender注入 $comment2 = new Comment(new TencentSender()); // 使用TencentSender發送郵件 $comment2->save(); // TencentSender ``` >上面的代碼,解決了`Comment`類對于`GmailSender`等具體類的依賴,通過構造函數,將相應的實現了 `EmailSenderInterface`接口的類實例傳入`Comment`類中,使得`Comment`類可以適用于不同的郵件服務。 從此以后,無論要使用何何種郵件服務,只需寫出新的`EmailSenderInterface`實現即可, `Comment`類的代碼不再需要作任何更改,擴展起來、測試起來都省心省力。 ###### 參考文獻 1、[Yii2.0 依賴注入容器(Dependency Injection Container)](https://www.yiichina.com/doc/guide/2.0/concept-di-container) 2、[Yii2.0 依賴注入(DI)和依賴注入容器的原理](https://www.yiichina.com/tutorial/112)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看