• <bdo id='ogXJr'></bdo><ul id='ogXJr'></ul>

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

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

      <legend id='ogXJr'><style id='ogXJr'><dir id='ogXJr'><q id='ogXJr'></q></dir></style></legend>
    1. 在 PHP 中执行多个构造函数的最佳方法

      Best way to do multiple constructors in PHP(在 PHP 中执行多个构造函数的最佳方法)
      1. <i id='ssJaU'><tr id='ssJaU'><dt id='ssJaU'><q id='ssJaU'><span id='ssJaU'><b id='ssJaU'><form id='ssJaU'><ins id='ssJaU'></ins><ul id='ssJaU'></ul><sub id='ssJaU'></sub></form><legend id='ssJaU'></legend><bdo id='ssJaU'><pre id='ssJaU'><center id='ssJaU'></center></pre></bdo></b><th id='ssJaU'></th></span></q></dt></tr></i><div id='ssJaU'><tfoot id='ssJaU'></tfoot><dl id='ssJaU'><fieldset id='ssJaU'></fieldset></dl></div>

        <legend id='ssJaU'><style id='ssJaU'><dir id='ssJaU'><q id='ssJaU'></q></dir></style></legend>

            <bdo id='ssJaU'></bdo><ul id='ssJaU'></ul>
              <tfoot id='ssJaU'></tfoot>
                <tbody id='ssJaU'></tbody>
              • <small id='ssJaU'></small><noframes id='ssJaU'>

                本文介绍了在 PHP 中执行多个构造函数的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                您不能在 PHP 类中放置两个具有唯一参数签名的 __construct 函数.我想这样做:

                You can't put two __construct functions with unique argument signatures in a PHP class. I'd like to do this:

                class Student 
                {
                   protected $id;
                   protected $name;
                   // etc.
                
                   public function __construct($id){
                       $this->id = $id;
                      // other members are still uninitialized
                   }
                
                   public function __construct($row_from_database){
                       $this->id = $row_from_database->id;
                       $this->name = $row_from_database->name;
                       // etc.
                   }
                }
                

                在 PHP 中执行此操作的最佳方法是什么?

                What is the best way to do this in PHP?

                推荐答案

                我可能会这样做:

                <?php
                
                class Student
                {
                    public function __construct() {
                        // allocate your stuff
                    }
                
                    public static function withID( $id ) {
                        $instance = new self();
                        $instance->loadByID( $id );
                        return $instance;
                    }
                
                    public static function withRow( array $row ) {
                        $instance = new self();
                        $instance->fill( $row );
                        return $instance;
                    }
                
                    protected function loadByID( $id ) {
                        // do query
                        $row = my_awesome_db_access_stuff( $id );
                        $this->fill( $row );
                    }
                
                    protected function fill( array $row ) {
                        // fill all properties from array
                    }
                }
                
                ?>
                

                然后,如果我想要一个我知道 ID 的学生:

                Then if i want a Student where i know the ID:

                $student = Student::withID( $id );
                

                或者如果我有一个 db 行数组:

                Or if i have an array of the db row:

                $student = Student::withRow( $row );
                

                从技术上讲,您不会构建多个构造函数,只是构建静态辅助方法,但您可以通过这种方式避免在构造函数中使用大量意大利面条式代码.

                Technically you're not building multiple constructors, just static helper methods, but you get to avoid a lot of spaghetti code in the constructor this way.

                这篇关于在 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 的问题)

              • <legend id='wOme5'><style id='wOme5'><dir id='wOme5'><q id='wOme5'></q></dir></style></legend>
                  <tbody id='wOme5'></tbody>

              • <small id='wOme5'></small><noframes id='wOme5'>

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