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 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳
  • ¥15 海康hlss视频流怎么播放
  • ¥15 Paddleocr:out of memory error on GPU
  • ¥30 51单片机C语言数码管驱动单片机为AT89C52