Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

单例模式疑惑?

<?php

class test{

protected function __construct(){

}

public static function getInstance(){
    static $_test;
    if (empty($_test)) {
        $_test = new test();
    }
    return $_test;
}

}

为什么在单例模式中该类要有一个public static的方法,而其他的方法都不能用public方法。这样做的目的是什么


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

简单的说,就是不让再次初始化了,也没有这个必要,保证就一个实例。需要什么方法,你调用得到的实例中的public方法啊。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...