duanlaiyin2356 2015-12-07 23:33
浏览 34
已采纳

Singleton配置类返回1

I have a problem. I made a singleton config class but when I have more than one variable to get, it returns 1.

example:

$config = Config::getInstance();

echo $config->get('database.host');

Works fine, returns "localhost".

BUT:

$config = Config::getInstance();

echo $config->get('database.host');
echo $config->get('database.user');
echo $config->get('database.pass');
echo $config->get('database.name');

returns "localhost", 1, 1, 1

Why is that? Here is my config class:

<?php

namespace System\Libraries;

class Config
{
    private static $_instance = null;

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

        return self::$_instance;
    }

    public function get($path)
    {
        if (isset($path)) {
            $path   = explode('.', $path);
            $config = require_once 'system/config/config.php';

            foreach ($path as $key) {
                if (isset($config[$key])) {
                    $config = $config[$key];
                }
            }

            return $config;
        }
    }

    private function __clone() {}
    private function __wakeup() {}
    private function __construct() {}

    public function __destruct()
    {
        self::$_instance = null;
    }
}

?>
  • 写回答

1条回答 默认 最新

  • dongqu9972 2015-12-08 01:43
    关注

    This is expected behavior. require_once will return true if the file has been required before. This behavior applies to all *_once functions in PHP, e.g. include_once. Echoing true displays 1 (false would display 0).

    You can fix this by loading the config file initially in the private constructor. This will also speed up your code, because the file doesn't have to be loaded every time you call get() on your Config class.

    class Config
    {
    
        /**
         * @var Config
         */
        private static $_instance = null;
    
        /**
         * @var array
         */
        private $config;
    
        /**
         * Config constructor.
         */
        private function __construct()
        {
            $this->config = require_once('system/config/config.php');
        }
    
        /**
         * Returns the instance.
         * 
         * @static
         * @return \Config
         */
        public static function getInstance()
        {
            if (self::$_instance == null) {
                self::$_instance = new Self;
            }
    
            return self::$_instance;
        }
    
        /**
         * Get a config item.
         * 
         * @param $path
         *
         * @return mixed
         */
        public function get($path)
        {
            if (isset($path)) {
                $path   = explode('.', $path);
                $config = $this->config;
    
                foreach ($path as $key) {
                    if (isset($config[$key])) {
                        $config = $config[$key];
                    }
                }
    
                return $config;
            }
        }
    
        private function __clone() {}
        private function __wakeup() {}
    
        public function __destruct()
        {
            self::$_instance = null;
        }
    }
    

    Note that the getInstance() method should be declared static.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向