类的选项值的最佳实践位置

时间:2022-10-05 11:27:47

With a PHP MVC project, I have a few model classes that load data from a RESTful web service, which requires an API key. I'd like to store the necessary API key in the central config file of my application.

在PHP MVC项目中,我有一些模型类可以从RESTful Web服务加载数据,这需要一个API密钥。我想将必要的API密钥存储在我的应用程序的*配置文件中。

To get that API key to my class, which method would be considered best?

要获得我的课程的API密钥,哪种方法最好?

  1. Put the value in $GLOBALS, so it can be accessed directly within the model classes which utilize it.
  2. 将值放在$ GLOBALS中,因此可以在使用它的模型类中直接访问它。

  3. Pass the value to the class upon instantiation.
  4. 在实例化时将值传递给类。

Method #2 seems best for OOP and code reuse, but method #1 is very convenient. I typically go with method #2, but I want to know if method #1 is generally considered to be an acceptable use of $GLOBALS, even in an OOP MVC environment.

方法#2似乎最适合OOP和代码重用,但方法#1非常方便。我通常使用方法#2,但我想知道方法#1是否通常被认为是$ GLOBALS的可接受用途,即使在OOP MVC环境中也是如此。

3 个解决方案

#1


3  

Symfony 1.4 uses an sfConfig::get('my_value'), which is like your first option, only wrapped in an object.

Symfony 1.4使用sfConfig :: get('my_value'),它就像你的第一个选项,只包装在一个对象中。

Symfony 2 uses dependency injection to reduce coupling, which is like your second option. Make sure you check out this link for a good explanation on dependency injection.

Symfony 2使用依赖注入来减少耦合,这就像你的第二个选择。请务必查看此链接,以获得有关依赖注入的详细说明。

However, reducing coupling increases complexity. So, it's really a tradeoff. I think the main point is to be consistent in your application. Whichever method is used can be changed, so long as you're consistent in its use.

但是,减少耦合会增加复杂性。所以,这真的是一种权衡。我认为重点是在您的应用程序中保持一致。无论使用哪种方法都可以更改,只要您的使用一致。

I would pick dependency injection. Not only is it easy to use if you use it all of the time, it's much more likely to lead to reusable code. The slowest part of a computer is the developer, so I try to build high quality, reusable components, wherever possible. I should also note that using dependency injection makes it much, much easier to build unit tests, should you choose to increase the quality and consistency of your code by adding unit tests.

我会选择依赖注入。如果您一直使用它,它不仅易于使用,而且更有可能导致可重用的代码。计算机中最慢的部分是开发人员,因此我尽可能地尝试构建高质量,可重用的组件。我还应该注意,如果您选择通过添加单元测试来提高代码的质量和一致性,那么使用依赖注入可以使构建单元测试变得更加容易。

Hope that helps...

希望有帮助......

#2


1  

I 'd say the best here would be a third option: use a global constant.

我会说这里最好的是第三种选择:使用全局常量。

define('API_KEY', 'foobar');

This is reasonable in the sense that the value of this constant is practically going to be unchanged during the lifetime of your application. A constant is better in that case because the value cannot be modified or overwritten through accident or malice. Plus, it does let you keep the convenience of a globally available value.

这是合理的,因为这个常数的值在您的应用程序生命周期中实际上将保持不变。在这种情况下,常数更好,因为不能通过意外或恶意修改或覆盖该值。此外,它确实让您保持全球可用价值的便利性。

#3


0  

You could also use class constants and/or statics:

您还可以使用类常量和/或静态:

class myClass
{
    const MY_API_KEY = 'somestringofstuff';

    // Or use a static array if you need access to multiple keys

    public static $apiKeys = array(
        'dev' => 'mydevapikey',
        'prod' => 'myprodapikey'
    );
}


// Calling code
$apiKey = myClass::MY_API_KEY;

// Or

$apiKey = myClass::$apiKeys['dev'];

#1


3  

Symfony 1.4 uses an sfConfig::get('my_value'), which is like your first option, only wrapped in an object.

Symfony 1.4使用sfConfig :: get('my_value'),它就像你的第一个选项,只包装在一个对象中。

Symfony 2 uses dependency injection to reduce coupling, which is like your second option. Make sure you check out this link for a good explanation on dependency injection.

Symfony 2使用依赖注入来减少耦合,这就像你的第二个选择。请务必查看此链接,以获得有关依赖注入的详细说明。

However, reducing coupling increases complexity. So, it's really a tradeoff. I think the main point is to be consistent in your application. Whichever method is used can be changed, so long as you're consistent in its use.

但是,减少耦合会增加复杂性。所以,这真的是一种权衡。我认为重点是在您的应用程序中保持一致。无论使用哪种方法都可以更改,只要您的使用一致。

I would pick dependency injection. Not only is it easy to use if you use it all of the time, it's much more likely to lead to reusable code. The slowest part of a computer is the developer, so I try to build high quality, reusable components, wherever possible. I should also note that using dependency injection makes it much, much easier to build unit tests, should you choose to increase the quality and consistency of your code by adding unit tests.

我会选择依赖注入。如果您一直使用它,它不仅易于使用,而且更有可能导致可重用的代码。计算机中最慢的部分是开发人员,因此我尽可能地尝试构建高质量,可重用的组件。我还应该注意,如果您选择通过添加单元测试来提高代码的质量和一致性,那么使用依赖注入可以使构建单元测试变得更加容易。

Hope that helps...

希望有帮助......

#2


1  

I 'd say the best here would be a third option: use a global constant.

我会说这里最好的是第三种选择:使用全局常量。

define('API_KEY', 'foobar');

This is reasonable in the sense that the value of this constant is practically going to be unchanged during the lifetime of your application. A constant is better in that case because the value cannot be modified or overwritten through accident or malice. Plus, it does let you keep the convenience of a globally available value.

这是合理的,因为这个常数的值在您的应用程序生命周期中实际上将保持不变。在这种情况下,常数更好,因为不能通过意外或恶意修改或覆盖该值。此外,它确实让您保持全球可用价值的便利性。

#3


0  

You could also use class constants and/or statics:

您还可以使用类常量和/或静态:

class myClass
{
    const MY_API_KEY = 'somestringofstuff';

    // Or use a static array if you need access to multiple keys

    public static $apiKeys = array(
        'dev' => 'mydevapikey',
        'prod' => 'myprodapikey'
    );
}


// Calling code
$apiKey = myClass::MY_API_KEY;

// Or

$apiKey = myClass::$apiKeys['dev'];