PHP - Application config file stored as - ini,php,sql,cached,php class,JSON,php array?

后端 未结 10 825
死守一世寂寞
死守一世寂寞 2020-12-13 14:15

I am trying to decide on the best way to store my applications configuration settings. There are so many options.

The majority of applications I have seen have used

10条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-13 14:56

    Just an example of how to implement a central XML/Xpath configuration.

    class Config {
        private static $_singleton;
        private $xml;
        static function getInstance() {
            if(is_null (self::$_singleton) ) {
                    self::$_singleton = new self;
            }
            return self::$_singleton;
        } 
        function open($xml_file) {
            $this->xml = simplexml_load_file($xml_file);
            return $this;
        }
        public function getConfig($path=null) {
            if (!is_object($this->xml)) {
                return false;
            }
            if (!$path) {
                return $this->xml;
            }
            $xml = $this->xml->xpath($path);
            if (is_array($xml)) {
                if (count($xml) == 1) {
                    return (string)$xml[0];
                }
                if (count($xml) == 0) {
                    return false;
                }
            }
            return $xml;
        }
    }
    

    Example call

    Config::getInstance()
        ->open('settings.xml')
        ->getConfig('/settings/module/section/item');
    

提交回复
热议问题