• <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 中的静态类初始化器

        时间:2023-06-22
        <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 中的静态类初始化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:__construct() 与 SameAsClassName() 用于 PHP 中的构造函数 下一篇:PHP 构造函数的目的

                  相关文章

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

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

                  1. <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>
                  2. <small id='VXlF3'></small><noframes id='VXlF3'>