• <i id='2iG2H'><tr id='2iG2H'><dt id='2iG2H'><q id='2iG2H'><span id='2iG2H'><b id='2iG2H'><form id='2iG2H'><ins id='2iG2H'></ins><ul id='2iG2H'></ul><sub id='2iG2H'></sub></form><legend id='2iG2H'></legend><bdo id='2iG2H'><pre id='2iG2H'><center id='2iG2H'></center></pre></bdo></b><th id='2iG2H'></th></span></q></dt></tr></i><div id='2iG2H'><tfoot id='2iG2H'></tfoot><dl id='2iG2H'><fieldset id='2iG2H'></fieldset></dl></div>

          <bdo id='2iG2H'></bdo><ul id='2iG2H'></ul>

        <small id='2iG2H'></small><noframes id='2iG2H'>

      1. <tfoot id='2iG2H'></tfoot>
      2. <legend id='2iG2H'><style id='2iG2H'><dir id='2iG2H'><q id='2iG2H'></q></dir></style></legend>

        带接口的 PHP 多重继承

        PHP Multiple Inheritance with Interfaces(带接口的 PHP 多重继承)
        <legend id='LKEG4'><style id='LKEG4'><dir id='LKEG4'><q id='LKEG4'></q></dir></style></legend>

          <tbody id='LKEG4'></tbody>

        <small id='LKEG4'></small><noframes id='LKEG4'>

            <tfoot id='LKEG4'></tfoot>
              <bdo id='LKEG4'></bdo><ul id='LKEG4'></ul>
                • <i id='LKEG4'><tr id='LKEG4'><dt id='LKEG4'><q id='LKEG4'><span id='LKEG4'><b id='LKEG4'><form id='LKEG4'><ins id='LKEG4'></ins><ul id='LKEG4'></ul><sub id='LKEG4'></sub></form><legend id='LKEG4'></legend><bdo id='LKEG4'><pre id='LKEG4'><center id='LKEG4'></center></pre></bdo></b><th id='LKEG4'></th></span></q></dt></tr></i><div id='LKEG4'><tfoot id='LKEG4'></tfoot><dl id='LKEG4'><fieldset id='LKEG4'></fieldset></dl></div>
                  本文介绍了带接口的 PHP 多重继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我一直在谷歌搜索,试图了解使用接口如何给我多重继承.

                  I'm trying to understand how using interfaces gives me multiple inheritance as I've been googling.

                  class A
                  {
                   function do1(){}
                   function do2(){}
                   function do3(){}
                  }
                  
                  class B extends A
                  {
                   function do4(){}
                   function do5(){}
                   function do6(){}
                  }
                  
                  class C extends B
                  {
                  }
                  

                  在上面的例子中,类 C 拥有类 A 和 B 的所有方法.然而,类 B 也拥有类 A 的所有方法,这是不必要的.

                  In the above example, class C has all the methods from class A and B. However, class B also has all the methods of class A, which is not necessary desired.

                  我的搜索结果是使用接口来解决这个问题,方法是将方法移动到类并创建接口,如下所示.

                  My searches have come up to use interfaces to solve this issue by moving methods to a class and creating interfaces, as below.

                  interface A
                  {
                       function do1();
                       function do2();
                       function do3();
                  }
                  
                  interface B
                  {
                       function do4();
                       function do5();
                       function do6();
                  }
                  
                  class C implements A, B
                  {
                       function do1(){}
                       function do2(){}
                       function do3(){}
                       function do4(){}
                       function do5(){}
                       function do6(){}
                  }
                  

                  我真的不明白这是如何解决问题的,因为所有代码都在新类中.如果我只是想像原来一样使用类 A,我将不得不创建一个实现接口 A 的新类,并将相同的代码复制到新类中.

                  I don't really see how this solves the issue because all the code is in the new class. If I just wanted to use class A as originally, I would have to create a new class that implement interface A and copy the same code to the new class.

                  有什么我遗漏的吗?

                  推荐答案

                  PHP 没有多重继承.但是,如果您有 PHP 5.4,您可以使用 traits 至少避免每个类都必须复制代码.

                  PHP doesn't have multiple inheritance. If you have PHP 5.4, though, you can use traits to at least avoid every class having to copy code.

                  interface A {
                      public function do1();
                      public function do2();
                      public function do3();
                  }
                  
                  trait Alike {
                      public function do1() { }
                      public function do2() { }
                      public function do3() { }
                  }
                  
                  
                  interface B {
                      public function do4();
                      public function do5();
                      public function do6();
                  }
                  
                  trait Blike {
                      public function do4() { }
                      public function do5() { }
                      public function do6() { }
                  }
                  
                  
                  class C implements A, B {
                      use Alike, Blike;
                  }
                  
                  class D implements A {
                      use Alike;
                  
                      // You can even "override" methods defined in a trait
                      public function do2() { }
                  }
                  

                  但是请注意,您必须实现接口并使用 trait(或者,当然,提供您自己的实现).并且 C 和 D 完全没有关系,除了两者都实现了 A 接口.Traits 基本上只是解释器级别的复制粘贴,不影响继承.

                  Note, though, you have to both implement the interface and use the trait (or, of course, provide your own implementation). And C and D are not related at all, except in both implementing the A interface. Traits are basically just interpreter-level copy and paste, and do not affect inheritance.

                  这篇关于带接口的 PHP 多重继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                      <small id='mXSrI'></small><noframes id='mXSrI'>

                        <tbody id='mXSrI'></tbody>
                      1. <legend id='mXSrI'><style id='mXSrI'><dir id='mXSrI'><q id='mXSrI'></q></dir></style></legend>

                        • <bdo id='mXSrI'></bdo><ul id='mXSrI'></ul>
                          <tfoot id='mXSrI'></tfoot>

                          • <i id='mXSrI'><tr id='mXSrI'><dt id='mXSrI'><q id='mXSrI'><span id='mXSrI'><b id='mXSrI'><form id='mXSrI'><ins id='mXSrI'></ins><ul id='mXSrI'></ul><sub id='mXSrI'></sub></form><legend id='mXSrI'></legend><bdo id='mXSrI'><pre id='mXSrI'><center id='mXSrI'></center></pre></bdo></b><th id='mXSrI'></th></span></q></dt></tr></i><div id='mXSrI'><tfoot id='mXSrI'></tfoot><dl id='mXSrI'><fieldset id='mXSrI'></fieldset></dl></div>