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

  • <tfoot id='pVyz4'></tfoot>

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

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

        <i id='pVyz4'><tr id='pVyz4'><dt id='pVyz4'><q id='pVyz4'><span id='pVyz4'><b id='pVyz4'><form id='pVyz4'><ins id='pVyz4'></ins><ul id='pVyz4'></ul><sub id='pVyz4'></sub></form><legend id='pVyz4'></legend><bdo id='pVyz4'><pre id='pVyz4'><center id='pVyz4'></center></pre></bdo></b><th id='pVyz4'></th></span></q></dt></tr></i><div id='pVyz4'><tfoot id='pVyz4'></tfoot><dl id='pVyz4'><fieldset id='pVyz4'></fieldset></dl></div>
      2. PHP 中的静态类初始化器

        Static class initializer in PHP(PHP 中的静态类初始化器)
        <tfoot id='pdvQY'></tfoot>

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

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

                  <tbody id='pdvQY'></tbody>
                  本文介绍了PHP 中的静态类初始化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个带有一些静态函数的辅助类.类中的所有函数都需要一个重"的初始化函数来运行一次(就像它是一个构造函数).

                  I have an helper class with some static functions. All the functions in the class require a ‘heavy’ initialization function to run once (as if it were a constructor).

                  是否有实现这一目标的良好做法?

                  Is there a good practice for achieving this?

                  我唯一想到的是调用一个 init 函数,如果它已经运行过一次(使用静态 $initialized 变量),就中断它的流程.问题是我需要在类的每一个函数上调用它.

                  The only thing I thought of was calling an init function, and breaking its flow if it has already run once (using a static $initialized var). The problem is that I need to call it on every one of the class’s functions.

                  推荐答案

                  听起来像使用单例而不是一堆静态方法更好

                  Sounds like you'd be better served by a singleton rather than a bunch of static methods

                  class Singleton
                  {
                    /**
                     * 
                     * @var Singleton
                     */
                    private static $instance;
                  
                    private function __construct()
                    {
                      // Your "heavy" initialization stuff here
                    }
                  
                    public static function getInstance()
                    {
                      if ( is_null( self::$instance ) )
                      {
                        self::$instance = new self();
                      }
                      return self::$instance;
                    }
                  
                    public function someMethod1()
                    {
                      // whatever
                    }
                  
                    public function someMethod2()
                    {
                      // whatever
                    }
                  }
                  

                  然后,在使用中

                  // As opposed to this
                  Singleton::someMethod1();
                  
                  // You'd do this
                  Singleton::getInstance()->someMethod1();
                  

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

                  <tfoot id='VXlF3'></tfoot>
                      <tbody id='VXlF3'></tbody>

                        <bdo id='VXlF3'></bdo><ul id='VXlF3'></ul>

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