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

      1. <small id='PmIEw'></small><noframes id='PmIEw'>

        <tfoot id='PmIEw'></tfoot>

        为什么我不能在 PHP 中重载构造函数?

        Why can#39;t I overload constructors in PHP?(为什么我不能在 PHP 中重载构造函数?)
          <bdo id='wgxEN'></bdo><ul id='wgxEN'></ul>

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

                  <tbody id='wgxEN'></tbody>

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

                • <i id='wgxEN'><tr id='wgxEN'><dt id='wgxEN'><q id='wgxEN'><span id='wgxEN'><b id='wgxEN'><form id='wgxEN'><ins id='wgxEN'></ins><ul id='wgxEN'></ul><sub id='wgxEN'></sub></form><legend id='wgxEN'></legend><bdo id='wgxEN'><pre id='wgxEN'><center id='wgxEN'></center></pre></bdo></b><th id='wgxEN'></th></span></q></dt></tr></i><div id='wgxEN'><tfoot id='wgxEN'></tfoot><dl id='wgxEN'><fieldset id='wgxEN'></fieldset></dl></div>
                  本文介绍了为什么我不能在 PHP 中重载构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经放弃了在 PHP 中重载构造函数的所有希望,所以我真正想知道的是为什么.

                  I have abandoned all hope of ever being able to overload my constructors in PHP, so what I'd really like to know is why.

                  有什么原因吗?它是否会创建本质上不好的代码?是被广泛接受的语言设计不允许,还是其他语言比 PHP 更好?

                  Is there even a reason for it? Does it create inherently bad code? Is it widely accepted language design to not allow it, or are other languages nicer than PHP?

                  推荐答案

                  您不能在 PHP 中重载任何方法.如果您希望能够在传递多个不同参数组合的同时实例化 PHP 对象,请使用带有私有构造函数的工厂模式.

                  You can't overload ANY method in PHP. If you want to be able to instantiate a PHP object while passing several different combinations of parameters, use the factory pattern with a private constructor.

                  例如:

                  public MyClass {
                      private function __construct() {
                      ...
                      }
                  
                      public static function makeNewWithParameterA($paramA) {
                          $obj = new MyClass(); 
                          // other initialization
                          return $obj;
                      }
                  
                      public static function makeNewWithParametersBandC($paramB, $paramC) {
                          $obj = new MyClass(); 
                          // other initialization
                          return $obj;
                      }
                  }
                  
                  $myObject = MyClass::makeNewWithParameterA("foo");
                  $anotherObject = MyClass::makeNewWithParametersBandC("bar", 3);
                  

                  这篇关于为什么我不能在 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 的问题)

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

                          <legend id='0hpij'><style id='0hpij'><dir id='0hpij'><q id='0hpij'></q></dir></style></legend>
                          <tfoot id='0hpij'></tfoot>
                          • <bdo id='0hpij'></bdo><ul id='0hpij'></ul>

                              <tbody id='0hpij'></tbody>