PHP如何访问GLOBALLY这个数组? [英] PHP How to access this array GLOBALLY?

查看:116
本文介绍了PHP如何访问GLOBALLY这个数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对PHP没有这么高级的认识,并且正在寻找最佳的解决方案:


  • 我有一个PHP数组,持有关于我的脚本的重要信息



我需要在脚本中经常重复使用。

  $ my_settings = array('width'=>'300','height'=>'200',...); 




  • 我将它存储在MySQL数据库中。


  • PHP脚本每次运行时都需要从MySQL获取该数组,以便知道如何组合视图。 $ b



(数组最终会变得很大......也许有几百行......)

在我的脚本的开头只有一次获取和定义这个数组的最佳做法是什么,所以我可以通过其他函数一直重复使用它......?



它是通过全局定义的吗?

  global $ my_settings; 
$ my_settings = get_my_settings_from_db();

函数returnSetting($ key =''){
global $ my_settings;
返回$ my_settings [$ key];

}

或者这是个坏主意?



最有效的方法是什么?



谢谢!

解决方案

而不是使用 global (这是一件坏事),您应该注入变量(或者甚至更好的部分)进入需要它的函数):

  $ my_settings = get_my_settings_from_db(); 

函数returnSetting($ settings,$ key =''){
return $ settings [$ key];
}

returnSetting($ my_settings,$ key);

或者更好的创建一个设置类:

  class Settings 
{
protected $ settings;

public function __construct($ settings)
{
$ this-> settings = $ settings;
}

public function getSetting($ key)
{
return $ this-> settings [$ key];



//你只为每个请求创建一个实例,所以你可以在例如你的引导文件
$ settings = new Settings($我的设置);

假设您有一个需要设置的类,您可以执行以下操作:

  class Foo 
{
protected $ settings;

public function __construct($ settings)
{
$ this-> settings = $ settings;
{

public function bar()
{
//做一些需要设置的事情
echo $ this-> settings-> getSetting ( '尺寸');
}
}

$ settings = new Settings($ my_settings);
$ foo = new Foo($ settings);
$ foo-> bar();

执行此操作时,您将使代码更易于测试和维护。



请注意,这只是我用啤酒一手书写的东西,没有经过测试,但您应该明白。 : - )

I'm not so advanced with PHP and am looking for the best solution to the following:

  • I have a PHP array that holds important information about my script that

I need to RE-USE this frequently in my script.

$my_settings = array('width'=>'300', 'height'=>'200', ...);

  • I store that in a MySQL db.

  • The PHP script needs to get that array from MySQL once each time it runs in order to know how to compose the view.

(The array will eventually become quite large... Maybe few hundred rows...)

What is the best practice to get and define this array only ONCE in the beginning of my script, so I can re-use it all the time by other functions etc...?

Is it by defining it globally?

global $my_settings;
$my_settings = get_my_settings_from_db();

function returnSetting($key='') {
 global $my_settings;
 return $my_settings[$key];

}

Or is this bad idea?

What is the most efficient way to do it?

Thanks!

解决方案

Instead of using global (which is a bad thing to do) you should inject the variable (or even better the part you need into the function that needs it):

$my_settings = get_my_settings_from_db();

function returnSetting($settings, $key='') {
     return $settings[$key];
}

returnSetting($my_settings, $key);

Or perhaps better create a settings class:

class Settings
{
    protected $settings;

    public function __construct($settings)
    {
        $this->settings = $settings;
    }

    public function getSetting($key)
    {
        return $this->settings[$key];
    }
}

// you only create one instance per request, so you would do this in for example your bootstrap file
$settings = new Settings($my_settings);

Let's say you have a class which needs some settings you could do the following:

class Foo
{
    protected $settings;

    public function __construct($settings)
    {
        $this->settings = $settings;
    {

    public function bar()
    {
        // do something which requires a setting
        echo $this->settings->getSetting('dimensions');
    }
}

$settings = new Settings($my_settings);
$foo = new Foo($settings);
$foo->bar();

When doing it this you will make you code better testable and maintainable.

Note this is just something I written with a beer in one hand and it is untested, but you should get the idea. :-)

这篇关于PHP如何访问GLOBALLY这个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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