将配置变量放入php 5中的类的最佳方法是什么?

时间:2021-02-24 20:10:03

This is for my DB class. I am new to OO, been a procedural lad for some time, so I'm still a bit murky.

这是我的DB类。我是OO的新手,一段时间以来一直是个程序员,所以我还是有点模糊。

My first idea was using a bunch of setter functions/methods.. but after writing a whole bunch, I thought about using PHP's define function, like so.

我的第一个想法是使用一堆setter函数/方法..但在写了一大堆之后,我想到了使用PHP的define函数,就像这样。

define('MYSQL_USERNAME', 'jimbo');

Is this an accepted practice? What is the best practice? Should I really clutter my class with a bunch of setter functions (I am currently the only developer using these classes). What are your solutions?

这是一种公认​​的做法吗?什么是最佳做法?我是否真的会用一堆setter函数使我的类变得杂乱(我目前是使用这些类的唯一开发人员)。你有什么解决方案?

Thank you!

3 个解决方案

#1


2  

I use const only for creating mnemonic names for immutable constants in the class. The define() function does not create constants as part of the class, it creates constants in the global space.

我只使用const来为类中的不可变常量创建助记符名称。 define()函数不会将常量作为类的一部分创建,它会在全局空间中创建常量。

class MyClass
{
  const CONFIG_FILE = 'myapp.ini';

Class configuration data I usually declare as a protected hash-array in the class. Keys are useful for mnemonics. Values are defaults.

类配置数据我通常在类中声明为受保护的哈希数组。键对于助记符很有用。值是默认值。

  protected $config = array(
    'logfile' => 'err.out',
    'debug' => false
  );

Then I load an "ini" format file with parse_ini_file() and use array_merge() to map the keys into your class config array:

然后我用parse_ini_file()加载一个“ini”格式文件,并使用array_merge()将键映射到类配置数组中:

  public function __construct() {
    $ini_data = parse_ini_file(self::CONFIG_FILE, __CLASS__);
    $this->config = array_merge($this->config, $ini_data);
  }

}

#2


2  

there are probably a few options to deal with this:

可能有一些选项可以解决这个问题:

  1. just use setters, it's perfectly acceptable, but can get a bit "wordy" with a lot of config options.

    只使用setter,它是完全可以接受的,但可以通过很多配置选项获得一些“罗嗦”。

  2. use a config object to pass in:

    使用配置对象传入:

    $config = (object) array(
       'prop1' => 'somevalue',
       'prop2' => 'somevalue2',
       'prop3' => 'somevalue3',
    );
    
    $db = new DB($config);
    
  3. if you want to use constants, you could restrict them to the class to avoid global namespace pollution:

    如果要使用常量,可以将它们限制在类中以避免全局命名空间污染:

    class DB {
        const USER = 'mysqluser';
    }
    
    echo DB::USER; // for example
    

#3


0  

I've had good success at doing this in two ways:

我通过两种方式取得了很大的成功:

  1. as @Owen recommends, using class constants

    正如@Owen建议的那样,使用类常量

    class Config {
        const PASSWORD_LENGTH = 12;
        const SEND_PASSWORD_EMAILS = true;
        // ...
    }
    
  2. For simple config variables (i.e. no arrays etc) the package vlucas/phpdotenv, available on composer, does a great job. A file .env contains all your config:

    对于简单的配置变量(即没有数组等),作曲家可用的包vlucas / phpdotenv做得很好。文件.env包含您的所有配置:

    PASSWORD_LENGTH=12
    SEND_PASSWORD_EMAILS=1
    

This is then made available via getenv() or the $_ENV superglobal.

然后通过getenv()或$ _ENV超全局提供。

    Dotenv::load(__DIR__);
    $passwordLength = $_ENV['PASSWORD_LENGTH']

#1


2  

I use const only for creating mnemonic names for immutable constants in the class. The define() function does not create constants as part of the class, it creates constants in the global space.

我只使用const来为类中的不可变常量创建助记符名称。 define()函数不会将常量作为类的一部分创建,它会在全局空间中创建常量。

class MyClass
{
  const CONFIG_FILE = 'myapp.ini';

Class configuration data I usually declare as a protected hash-array in the class. Keys are useful for mnemonics. Values are defaults.

类配置数据我通常在类中声明为受保护的哈希数组。键对于助记符很有用。值是默认值。

  protected $config = array(
    'logfile' => 'err.out',
    'debug' => false
  );

Then I load an "ini" format file with parse_ini_file() and use array_merge() to map the keys into your class config array:

然后我用parse_ini_file()加载一个“ini”格式文件,并使用array_merge()将键映射到类配置数组中:

  public function __construct() {
    $ini_data = parse_ini_file(self::CONFIG_FILE, __CLASS__);
    $this->config = array_merge($this->config, $ini_data);
  }

}

#2


2  

there are probably a few options to deal with this:

可能有一些选项可以解决这个问题:

  1. just use setters, it's perfectly acceptable, but can get a bit "wordy" with a lot of config options.

    只使用setter,它是完全可以接受的,但可以通过很多配置选项获得一些“罗嗦”。

  2. use a config object to pass in:

    使用配置对象传入:

    $config = (object) array(
       'prop1' => 'somevalue',
       'prop2' => 'somevalue2',
       'prop3' => 'somevalue3',
    );
    
    $db = new DB($config);
    
  3. if you want to use constants, you could restrict them to the class to avoid global namespace pollution:

    如果要使用常量,可以将它们限制在类中以避免全局命名空间污染:

    class DB {
        const USER = 'mysqluser';
    }
    
    echo DB::USER; // for example
    

#3


0  

I've had good success at doing this in two ways:

我通过两种方式取得了很大的成功:

  1. as @Owen recommends, using class constants

    正如@Owen建议的那样,使用类常量

    class Config {
        const PASSWORD_LENGTH = 12;
        const SEND_PASSWORD_EMAILS = true;
        // ...
    }
    
  2. For simple config variables (i.e. no arrays etc) the package vlucas/phpdotenv, available on composer, does a great job. A file .env contains all your config:

    对于简单的配置变量(即没有数组等),作曲家可用的包vlucas / phpdotenv做得很好。文件.env包含您的所有配置:

    PASSWORD_LENGTH=12
    SEND_PASSWORD_EMAILS=1
    

This is then made available via getenv() or the $_ENV superglobal.

然后通过getenv()或$ _ENV超全局提供。

    Dotenv::load(__DIR__);
    $passwordLength = $_ENV['PASSWORD_LENGTH']