doujiongqin0687 2014-04-25 07:36
浏览 23
已采纳

php - 启动构造函数

If I have a class with only static methods, what is the best way to initialize the constructor in that class, for example:

Class Example {
    public function __construct(){
        /*code here*/
    }
    public static function method1()
        /*code here*/
    }
    public static function method2() {
        /*code here*/
    }
}

Example::method1();

The constructor didn't get initiated, what is the best way to do that?

  • 写回答

6条回答 默认 最新

  • doufu1504 2014-04-25 07:49
    关注

    Although the whole solution is bad, you can have a private static method which is called in each other public static method.

    This is not a constructor, and it's normally, because the constructor is used to construct and object, and you don't have one in static context.

    So, for firing a common functionality after each static call, you can use:

    class Example {
        private static function common() {
            echo 'called';
        }
        public static function method1() {
            self::common();
            echo "</br> method1;";
        }
        public static function method2() {
            self::common();
            echo "</br> method2;";
        }
    }
    
    Example::method2();
    

    Which results into

    called
    method2;
    

    You can also build your object into the static method

    class Example {
        private function __construct() {
            echo 'contructor called';
        }
        public static function method1() {
            $self = new self();
            echo "</br> method1;";
        }
        public static function method2() {
            $self = new self();
            echo "</br> method2;";
        }
    }
    
    Example::method1();
    

    Results into:

    contructor called
    method1;
    

    The problem here is that each method will can new instance of the constructor.

    You can use the Singleton pattern as suggested, in order to have shared instance of the class in all your static methods.

    class Example {
    
        private static $_inst = null;
    
        private $_x = 0;
    
        private function __construct() {
            echo 'contructor called';
        }
    
        private static function getInstance() {
            if(self::$_inst == null) {
                self::$_inst = new self();
            }
            return self::$_inst;
        }
    
        public static function method1() {
            self::getInstance();
            self::getInstance()->_x = 100;
            echo "</br> method1;";
        }
        public static function method2() {
            self::getInstance();
            echo self::getInstance()->_x;
            echo "</br> method2;";
        }
    }
    
    Example::method1();
    Example::method2();
    

    Which will result into:

    contructor called
    method1;100
    method2;
    

    So the value of instance variable $_x is saved when method2() is called, but the constructor is not called second time as you can see there is only one contructor called in the result.

    And, to repeat myself, this whole idea is horrible.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题