单例配置 class returns 1
Singleton config class returns 1
我有问题。我做了一个单例配置 class 但是当我有多个变量要获取时,它 returns 1.
示例:
$config = Config::getInstance();
echo $config->get('database.host');
工作正常,returns "localhost"。
但是:
$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
这是为什么?这是我的配置 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;
}
}
?>
这是预期的行为。 require_once
将 return true
如果之前需要该文件。此行为适用于 PHP 中的所有 *_once 函数,例如include_once
。回显 true
显示 1
(false
将显示 0
)。
您可以通过最初在私有构造函数中加载配置文件来解决此问题。这也将加速您的代码,因为每次您在 Config class.
上调用 get()
时不必加载该文件
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;
}
}
请注意,getInstance()
方法应声明为静态的。
我有问题。我做了一个单例配置 class 但是当我有多个变量要获取时,它 returns 1.
示例:
$config = Config::getInstance();
echo $config->get('database.host');
工作正常,returns "localhost"。
但是:
$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
这是为什么?这是我的配置 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;
}
}
?>
这是预期的行为。 require_once
将 return true
如果之前需要该文件。此行为适用于 PHP 中的所有 *_once 函数,例如include_once
。回显 true
显示 1
(false
将显示 0
)。
您可以通过最初在私有构造函数中加载配置文件来解决此问题。这也将加速您的代码,因为每次您在 Config class.
上调用get()
时不必加载该文件
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;
}
}
请注意,getInstance()
方法应声明为静态的。