PHP应用程序全局设置 [英] php application global settings

查看:135
本文介绍了PHP应用程序全局设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了几乎所有关于此主题在StackOverflow上找到的问题,但找不到直接答案。



这是我的代码:



应用程序类

 <?php 
类Application extends Settings {
public function __construct($ env,$ cacheDir,$ configFile){
self :: $ _ env = $ env;
self :: $ _ cacheDir = $ cacheDir;
self :: $ _ config = $ this-> loadConfig($ configFile)//将配置文件从xml文件读入配置对象
}

//其他方法
}
?>

设置类:

 <?php 
class Settings {
protected static $ _env = null;
protected static $ _cacheDir = null;
protected static $ _config = null;

public static function getEnv(){
return self :: $ _ env;
}

public static function getCacheDir(){
return self :: $ _ cacheDir;
}

public static function getConfig(){
return self :: $ _ config;
}
}
?>

我从代码中的任何位置访问设置,如下所示:

 <?php 
var_dump(Settings :: getEnv());
?>

我想从许多不同的地方访问设置。所有值只能设置一次,不能被覆盖(所以__set方法的注册表不起作用,因为我可以在应用程序的任何阶段从任何地方设置任何值)



问题:

是否存储像这样的全局设置是一种好的做法。这种方法有什么缺点?
也许有更好的方法来做到这一点吗?

感谢您的回答

解决方案您的应用程序类不应该扩展设置,因为两者之间没有关系类。相反,您应该使用依赖项注入将设置包含到应用程序中 class。下面是一个例子,我建议阅读依赖注入。

  class Settings {
// public为了简化示例,您可以添加setter和getters
public $ _env = null;
public $ _cacheDir = null;
public $ _config = null;
}

class Application {
protected $ config;

public function setConfig($ config){
$ this-> config = $ config;
}

}



$ app = new Application();

$ config = new Settings();

$ config-> _env ='dev';
$ config-> _cacheDir ='/ my / dir';
$ config-> _config = array(/ * Config here * /);

$ app-> setConfig($ config);

正如marcelog在另一个答案中提到的,您可以使用引导类来处理注入配置,以及其他对象添加到 Application 类中。



引导类的基本示例:

  class Bootstrap {

protected $ application;

public function __construct(Application $ app){
$ this-> application = $ app;


// connivence method
public function init(){
$ this-> initSettings();


public function initSettings(){
$ settings = new Settings();
$ settings-> _env ='dev';
$ settings-> _cacheDir ='/ my / dir';

$ config = array(); //从这里的文件加载配置
$ settings-> _config = config;
$ this-> application-> setSettings($ settings);
}

//其他init方法
}

$ app = new Application();

$ bootstrap = new Bootstrap($ app);

$ bootstrap-> init();

这些都是非常基本的例子,没有什么能够阻止你编写神奇的getter和setter,调用以init开头的任何方法等。

I have read almost all question I have found on StackOverflow on this topic, but could not find a straight answer.

Here is my code:

Application class

<?php
    class Application extends Settings {
        public function __construct($env, $cacheDir, $configFile) {
            self::$_env = $env;
            self::$_cacheDir = $cacheDir;
            self::$_config = $this->loadConfig($configFile) // reads configs from xml file into Config object
        }

        // other methods
    }
?>

Settings class:

<?php
class Settings {
    protected static $_env = null;
    protected static $_cacheDir = null;
    protected static $_config = null;

    public static function getEnv() {
        return self::$_env;
    }

    public static function getCacheDir() {
        return self::$_cacheDir;
    }

    public static function getConfig() {
        return self::$_config;
    }
}
?>

I access settings from anywhere in my code like this:

<?php
var_dump(Settings::getEnv());
?>

I want to access Settings form many different places. All values can be set only once and cannot be overwritten (so registry with __set methods do not work, because I can set any value from any place in any stage of application process)

Questions:

Is it good practice to store global settings like this. What downsides of this method? Maybe there's a much better way to do this?

Thank you for your answers

解决方案

Your Application class should not extend Settings as there is no relationship between the two classes. Instead you should use dependency injection to include the settings into the Application class. There is an example of this below and I recommend reading up on dependency injection.

class Settings {
    // public to simplify example, you can add setters and getters
    public $_env = null;
    public $_cacheDir = null;
    public $_config = null;
}

class Application {
    protected $config;

    public function setConfig($config) {
        $this->config = $config;
    }

}



$app = new Application();

$config = new Settings();

$config->_env = 'dev';
$config->_cacheDir = '/my/dir';
$config->_config = array(/* Config here */);

$app->setConfig($config);

As mentioned by marcelog in another answer you could use a bootstrap class to handle the injection of the config, as well as other objects, into your Application class.

A basic example of a bootstrap class:

class Bootstrap {

    protected $application;

    public function __construct(Application $app) {
        $this->application = $app;
    }

    // connivence method
    public function init() {
        $this->initSettings();
    }

    public function initSettings() {
        $settings = new Settings();
        $settings->_env = 'dev';
        $settings->_cacheDir = '/my/dir';

        $config = array(); // load config from file here
        $settings->_config = config;
        $this->application->setSettings($settings);
    }

    // other init methods
}

$app = new Application();

$bootstrap = new Bootstrap($app);

$bootstrap->init();

These are very basic examples and there is nothing stopping you from writing magic getters and setters, having the bootstrap call any method that begins with init, etc...

这篇关于PHP应用程序全局设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆