开发者

PHP - Will an XML-parsing singleton class only parse the XML once?

I have a class which reads some settings from an XML file with simplexml. If I build it in the singleton style and save those settings in a publicly accessible array, does that mean it would effectively retrieve the file only once?

Basically, in a simplified form, this:

class myClass {
    public $_requestConfiguration;
    public $_conditions;
    public $_requestSets;

    开发者_Go百科private static $_instance;

    private function __construct() {
        $configFile = simplexml_load_file(APPLICATION_PATH.'/configs/chapter_requests.xml');
        $this->_requestConfiguration = $configFile->requests->request;
        $this->_conditions = $configFile->conditions;
        $this->_requestSets = $configFile->request_sets;
    }

    public static function getInstance() {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
}


You will indeed have only one instance and the XML file will only be read once for the execution of the script.

When the script is done executing everything will be removed from memory and on the next run your 'singleton' will start reading the XML again because the static instance is no longer stored in memory.

I've somewhat asked the same question over here.


Yes.

Adding comical banter to reach minimum of 30 characters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜