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

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

      1. <tfoot id='Vs57q'></tfoot>
      2. 在 PHP5 类中,何时调用私有构造函数?

        In a PHP5 class, when does a private constructor get called?(在 PHP5 类中,何时调用私有构造函数?)

            <tbody id='QR5R5'></tbody>

            <tfoot id='QR5R5'></tfoot>

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

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

                • 本文介绍了在 PHP5 类中,何时调用私有构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我正在编写一个 PHP (>= 5.0) 类,它是一个单例.我读过的所有文档都说要将类构造函数设为私有,这样就无法直接实例化该类.

                  Let's say I'm writing a PHP (>= 5.0) class that's meant to be a singleton. All of the docs I've read say to make the class constructor private so the class can't be directly instantiated.

                  所以如果我有这样的事情:

                  So if I have something like this:

                  class SillyDB
                  {
                    private function __construct()
                    {
                  
                    }
                  
                    public static function getConnection()
                    {
                  
                    }
                  }
                  

                  除了我在做一个

                  new SillyDB() 
                  

                  在类本身内部调用?

                  为什么我完全可以从内部实例化 SillyDB?

                  And why am I allowed to instantiate SillyDB from inside itself at all?

                  推荐答案

                  __construct() 只有在您从包含私有构造函数的类的方法中调用它时才会被调用.所以对于你的单身人士,你可能有这样的方法:

                  __construct() would only be called if you called it from within a method for the class containing the private constructor. So for your Singleton, you might have a method like so:

                  class DBConnection
                  {
                     private static $Connection = null;
                  
                     public static function getConnection()
                     {
                        if(!isset(self::$Connection))
                        {
                           self::$Connection = new DBConnection();
                        }
                        return self::$Connection;
                     }
                  
                     private function __construct()
                     {
                  
                     }
                  }
                  
                  $dbConnection = DBConnection::getConnection();
                  

                  您能够/希望从自身内部实例化类的原因是,您可以检查以确保在任何给定时间只存在一个实例.毕竟,这就是单身人士的全部意义所在.对数据库连接使用单例可确保您的应用程序不会一次建立大量的数据库连接.

                  The reason you are able/would want to instantiate the class from within itself is so that you can check to make sure that only one instance exists at any given time. This is the whole point of a Singleton, after all. Using a Singleton for a database connection ensures that your application is not making a ton of DB connections at a time.

                  按照@emanuele-del-grande 的建议添加了 $

                  Added $, as suggested by @emanuele-del-grande

                  这篇关于在 PHP5 类中,何时调用私有构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='kSEJS'></small><noframes id='kSEJS'>

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

                      1. <tfoot id='kSEJS'></tfoot>
                            <bdo id='kSEJS'></bdo><ul id='kSEJS'></ul>