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.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥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