设置一个配置 PHP 类来保留项目设置是否正确?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10987703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:32:14  来源:igfitidea点击:

Is it right to set a config PHP class to keep project settings?

phpclassconfigurationconfig-files

提问by Amr

I want to create a config.php file to keep the various configuration values that usually being changed from project to project, and I want to define a class to keep the config values in this file like the following:

我想创建一个 config.php 文件来保留通常从项目到项目更改的各种配置值,并且我想定义一个类来保留这个文件中的配置值,如下所示:

class Config {
    const DB_SERVER    = 'localhost',
          DB_NAME      = 'abc',
          DB_USERNAME  = 'admin',
          DB_PASSWORD  = '12345',

          WEBSITE_NAME = 'My New Website',
          IMAGE_DIR    = 'img';
}

and so on, I want to define all values as constants inside the class, and I will call them like the following:

依此类推,我想将所有值定义为类中的常量,我会像下面这样调用它们:

$connection = mysql_connect(Config::DB_SERVER, Config::DB_USERNAME, Config::DB_PASSWORD) or die("Database connection failed..");

I want to know: Is this way of setting the project configuration is right? Does this way have any cons? And if it was wrong, then what the best way to do this?

我想知道:这样设置项目配置对吗?这种方式有什么缺点吗?如果这是错误的,那么最好的方法是什么?

采纳答案by Amr

I've found these 2 articles that are talking about the same topic and I found that they are very useful so I wanted to share them here:

我发现这两篇文章都在讨论同一主题,我发现它们非常有用,所以我想在这里分享它们:

1- Using PHP classes to store configuration data
2- Using a PHP Class to Store Configuration

1-使用 PHP 类存储配置数据
2-使用 PHP 类存储配置

I hope they help as they did for me.

我希望他们能像他们为我所做的那样提供帮助。

回答by cHao

It's one way of doing it, yes. A not-too-bad way, IMO. Basically the class becomes a config file, just with PHP syntax.

这是一种方法,是的。一种不错的方式,IMO。基本上这个类变成了一个配置文件,只是用 PHP 语法。

There are a couple of drawbacks, though:

但是,有几个缺点:

  • You can't have const arrays orobjects. (Course, you can't have global constant arrays/objects either, so...) (Since 5.6, you can have constant arrays. Still no const objects, though. I'm pretty sure you can't have const resources either, as that wouldn't make much sense.)

    You can work around this by implementing a static getter for objects (which of course is coded to always return the same object)...but i'd recommend against it in most cases. It is only a safe option if the objects in your config are immutable by design. (Objects that aren't designed for immutability are way too easy to change, even by accident.)

    (Aside from the mutability issue, it bugs me having actual running code in a config file...but that's mostly personal preference.)

  • This class has a different purpose than the rest -- it's intendedto change per project. You might consider keeping the Config class somewhere apart from the rest of the classes, like where you'd normally keep a config file.

  • With a real config file, since you parse it at runtime, you could conceivably deal with a missing or invalid file (say, by running with default settings, using what parts are parsable, and/or showing a useful error message). But once your configuration is running as PHP code, any syntax errors -- or, if you haven't accounted for it, a missing Config class -- will stop the app dead in its tracks. And if you're running with display_errorsoff (recommended in production), the problem might be less than obvious.

  • 你不能有 const数组或对象。(当然,你也不能拥有全局常量数组/对象,所以......)(从 5.6 开始,你可以拥有常量数组。但是仍然没有常量对象。我很确定你也不能拥有常量资源,因为那没有多大意义。)

    您可以通过为对象实现静态 getter 来解决这个问题(当然,它被编码为始终返回相同的对象)……但我建议在大多数情况下反对它。如果您的配置中的对象在设计上是不可变的,那么这只是一个安全的选择。(不是为不变性设计的对象太容易改变,即使是偶然的。)

    (除了可变性问题,它让我在配置文件中有实际运行代码的问题……但这主要是个人偏好。)

  • 这个类与其他类有不同的目的——它旨在针对每个项目进行更改。您可能会考虑将 Config 类与其他类分开保存,例如您通常会保存配置文件的地方。

  • 使用真实的配置文件,因为您在运行时解析它,您可以想象处理丢失或无效的文件(例如,通过使用默认设置运行,使用可解析的部分,和/或显示有用的错误消息)。但是一旦您的配置作为 PHP 代码运行,任何语法错误——或者,如果您没有考虑到它,缺少 Config 类——都会使应用程序停止运行。如果您正在运行display_errors(推荐在生产中),问题可能不那么明显。

回答by erikbwork

I think, what you want is the static-keyword!

我想,你想要的是静态关键字!

class Config {
    static $DB_SERVER    = 'localhost';
    static $DB_NAME      = 'abc';
    static $DB_USERNAME  = 'admin';
    static $DB_PASSWORD  = '12345';

    static $WEBSITE_NAME = 'My New Website';
    static $IMAGE_DIR    = 'img';
}

like that. You cann call them with ::, e.g. Config::$DB_SERVER.

像那样。你不能用::,例如来调用它们Config::$DB_SERVER

Btw. normally you don't write them big like that if they are class variables. Big means globals, usually.

顺便提一句。如果它们是类变量,通常你不会把它们写得那么大。通常,大意味着全局变量。