编辑PHP文件以通过PHP进行配置

时间:2023-01-15 09:32:01

First of all, I am assuming this is NOT bad practice due to various popular software using this method, such as SMF.

首先,我假设这是一个不错的做法,因为使用这种方法的各种流行软件,如SMF。

Anyway, so I currently have this code:

无论如何,所以我目前有这个代码:

<?php
// main visual config
$cfg['lang']    = 'en';

// paths
$path['admin']  = 'index.php?p=admin';
$path['admin2'] = 'zvfpcms/admin';
$path['root']   = 'zvfpcms';
$path['images'] = 'zvfpcms/img';
$path['css']    = 'zvfpcms/css';
$path['js']     = 'zvfpcms/js';
$path['pages']  = 'zvfpcms/pg';
?>

The thing is, I want the $cfg variable(s) to be edited directly via an interface.

问题是,我希望通过接口直接编辑$ cfg变量。

How would this be achieved? I cannot think replacing strings would be a good idea especially when there are a very large number of possibilities (or even infinite) for future $cfg variables I create.

如何实现?我不能认为替换字符串是一个好主意,特别是当我创建的未来$ cfg变量存在大量可能性(甚至无限)时。

Or, will I have to settle for a different method...

或者,我是否必须采取不同的方法......

Answers appreciated!

5 个解决方案

#1


Pear Config will let you read and write configuration easily to/from different sources, including a PHP file/array. You would probably need to move $cfg into its own file though and include it so that other variables etc. are unaffected.

Pear Config将允许您轻松地从/从不同的源(包括PHP文件/阵列)读取和写入配置。你可能需要将$ cfg移动到它自己的文件中并包含它以便其他变量等不受影响。

You could also do it yourself using var_export() and then writing over the file, again you would probably need to move the variable into its own file. I think that this would write messier/less readable PHP than the Pear class.

你也可以自己使用var_export()然后在文件上写一遍,你可能需要将变量移动到自己的文件中。我认为这会比Pear类编写更麻烦/更不易读的PHP。

#2


Just have all the settings in your database, and when a setting changes, rebuild the config file.

只需拥有数据库中的所有设置,当设置更改时,重建配置文件。

#3


Don't have your PHP edit PHP code. Use an XML config file instead. Read the XML info out, if you want to make a change, change the value in the data structure generated from the XML file and spit it back out into a new config file.

没有PHP编辑PHP代码。请改用XML配置文件。读取XML信息,如果要进行更改,请更改从XML文件生成的数据结构中的值,然后将其吐回到新的配置文件中。

#4


Either store the settings in a database, or you could use a standard ini-file (key=value pairs);

将设置存储在数据库中,或者您可以使用标准的ini文件(键=值对);

lang=en
timezone=CET

Read them:

function loadSettings() {
    $cfg = array();
    foreach (file('settings.ini') as $line) {
        list ($key, $value) = explode('=', $line);
        $cfg[$key] = $value;
    }
    return $cfg;
}

File I/O are less efficiant than standard database calls thou, especially if you already have an open connection to a database.

文件I / O的效率低于标准数据库调用,特别是如果您已经打开了与数据库的连接。

#5


The reason I believe that this IS bad practice is because you can have a running system before the edit, have some error in the edit (perhaps you didn't escape a single-quote character) and after the edit the entire application becomes unavailable because PHP can't parse the config file, which affects almost everything you do.

我认为这是一个不好的做法的原因是因为你可以在编辑之前有一个正在运行的系统,在编辑中有一些错误(也许你没有逃避单引号字符)并且在编辑之后整个应用程序变得不可用,因为PHP无法解析配置文件,这几乎影响了您所做的一切。

The XML suggestion isn't a bad one as you can handle any errors that occur while you're reading the XML.

XML建议并不错,因为您可以处理在读取XML时发生的任何错误。

If you had a database (reading this post it looks like you aren't using one, which is fine), that would be the best place for the data - that's what it's for. In a flat-file world, reading in a non-PHP file and interpreting it is far better than auto-editing the PHP files yourself.

如果你有一个数据库(阅读这篇文章看起来你没有使用它,这很好),这将是数据的最佳位置 - 这就是它的用途。在平面文件世界中,读取非PHP文件并解释它比自己编辑PHP文件要好得多。

#1


Pear Config will let you read and write configuration easily to/from different sources, including a PHP file/array. You would probably need to move $cfg into its own file though and include it so that other variables etc. are unaffected.

Pear Config将允许您轻松地从/从不同的源(包括PHP文件/阵列)读取和写入配置。你可能需要将$ cfg移动到它自己的文件中并包含它以便其他变量等不受影响。

You could also do it yourself using var_export() and then writing over the file, again you would probably need to move the variable into its own file. I think that this would write messier/less readable PHP than the Pear class.

你也可以自己使用var_export()然后在文件上写一遍,你可能需要将变量移动到自己的文件中。我认为这会比Pear类编写更麻烦/更不易读的PHP。

#2


Just have all the settings in your database, and when a setting changes, rebuild the config file.

只需拥有数据库中的所有设置,当设置更改时,重建配置文件。

#3


Don't have your PHP edit PHP code. Use an XML config file instead. Read the XML info out, if you want to make a change, change the value in the data structure generated from the XML file and spit it back out into a new config file.

没有PHP编辑PHP代码。请改用XML配置文件。读取XML信息,如果要进行更改,请更改从XML文件生成的数据结构中的值,然后将其吐回到新的配置文件中。

#4


Either store the settings in a database, or you could use a standard ini-file (key=value pairs);

将设置存储在数据库中,或者您可以使用标准的ini文件(键=值对);

lang=en
timezone=CET

Read them:

function loadSettings() {
    $cfg = array();
    foreach (file('settings.ini') as $line) {
        list ($key, $value) = explode('=', $line);
        $cfg[$key] = $value;
    }
    return $cfg;
}

File I/O are less efficiant than standard database calls thou, especially if you already have an open connection to a database.

文件I / O的效率低于标准数据库调用,特别是如果您已经打开了与数据库的连接。

#5


The reason I believe that this IS bad practice is because you can have a running system before the edit, have some error in the edit (perhaps you didn't escape a single-quote character) and after the edit the entire application becomes unavailable because PHP can't parse the config file, which affects almost everything you do.

我认为这是一个不好的做法的原因是因为你可以在编辑之前有一个正在运行的系统,在编辑中有一些错误(也许你没有逃避单引号字符)并且在编辑之后整个应用程序变得不可用,因为PHP无法解析配置文件,这几乎影响了您所做的一切。

The XML suggestion isn't a bad one as you can handle any errors that occur while you're reading the XML.

XML建议并不错,因为您可以处理在读取XML时发生的任何错误。

If you had a database (reading this post it looks like you aren't using one, which is fine), that would be the best place for the data - that's what it's for. In a flat-file world, reading in a non-PHP file and interpreting it is far better than auto-editing the PHP files yourself.

如果你有一个数据库(阅读这篇文章看起来你没有使用它,这很好),这将是数据的最佳位置 - 这就是它的用途。在平面文件世界中,读取非PHP文件并解释它比自己编辑PHP文件要好得多。