<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。現在實中的接口, 不同的種類,但是有相同的能力。 家里耗子多。 買一只狗,或狗都可以,都可以讓他們去干這個活。 用哪個看個人喜歡。 ![](https://img.kancloud.cn/07/73/0773c58b59b7aaef32496fd33f6cd82a_500x500.png) 2。程序 ``` <pre class="calibre7">``` //發送接口 interface send { //實現這個接口的類,必須實現,接口定義的方法 public function send($str); } // implements 表示 實現發送接口 class Sms implements send { public $to = '13912345678'; public function send($str) { var_dump("{$this->to}, $str"); } } class Email implements send { public $to = '12345678@qq.com'; public function send($str) { var_dump("{$this->to}, $str"); } } class Order { public $notice; public function __construct($notice) { $this->notice = $notice; } public function delivery() { $this->notice->send('發現訂單未支付,但是發貨了'); } } $order = new Order(new Sms()); $order->delivery(); $order = new Order(new Email()); $order->delivery(); ``` ``` 輸出: 13912345678, 發現訂單未支付,但是發貨了 [12345678@qq.com](#!), 發現訂單未支付,但是發貨了 - - - - - - 3。疑問,不一定要用接口啊, Sms 跟 Email 實現 send方法就可以接比如這樣。程序一樣正常運行。 ``` <pre class="calibre7">``` class Sms { public $to = '13912345678'; public function send($str) { var_dump("{$this->to}, $str"); } } class Email { public $to = '12345678@qq.com'; public function send($str) { var_dump("{$this->to}, $str"); } } ``` ``` 3。但是如果 Order 強制了接口,那么Sms 跟 Email 就必須實現 接口,不然會報錯 ``` <pre class="calibre7">``` class Order { public $notice; //參數為 send $notice ,表示 $notice 必須實現了 send接口,不然程序報錯 public function __construct(send $notice) { $this->notice = $notice; } public function delivery() { $this->notice->send('發現訂單未支付,但是發貨了'); } } ``` ``` 4。那問題來了,寫程序的時候,不強制要求就可以啦 。分析有兩種情況吧。 - - - - - - 一、項目主管比較嚴謹,分配下面的人分別 實現Sms ,跟 Email功能,必須實現這個接口。 - - - - - - 二、PHP一些內置的功能也要實現相應的接口,比如:foreach 是遍歷數組 的,如果對象,也要可以被 foreach遍歷,那么對象就必須實現 Iterator 接口。
                  <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>

                              哎呀哎呀视频在线观看