Better solutions
Many other configuration storage formats are better suited for this kind of thing. Look into php
file returns array, ini
, json
, xml
or yaml
.
"PHP
file returns array" is a simple PHP file which looks like this
return(
array(
'config_key' => 'config_value'
)
);
?>
The return value of this file can be retrieved by your code when you're including it: $x = include('file.php');
and $x
will have the values in the array.
INI
is simple & intuitive to read or write for humans. It has a limited structure. PHP can read it with one function but it has write capabilities only in a separate (non-default) package. Which means you have to generate the ini files yourself.
JSON
is "fairly" simple & intuitive to read or write for humans. It has a flexible structure extensible structure. PHP can read and write to a JSON
file using only a couple of functions. Unfortunately PHP does not retain JSON
pretty-print formatting so after you overwrite a file, it'll be all in one line, harder to read after that.
XML
is simple & intuitive to read for humans, it can be very informative because it's quite verbose about what everything is. I has a structure almost as flexible as JSON
and it is extensible. PHP can read and write to an XML
but doing so means using a handful of lines of code (5-10 for simple stuff).
YAML
is another option which is easy to read and write for humans, PHP doesn't have direct YAML
support but there are other options (see below). It's structure is flexible and extensible. For me personally understanding YAML
has been less intuitive.
Zend_Config
can be used as an interface to reading/writing any of the above formats and can be used to abstract the file format itself and offer your application a format-agnostic way of handling configurations.
You could also use the database to store your configuration or a separate SQLite database dedicated to storing configurations in general -- this is usually used when many configurations need to be retained in a fine grained searchable format that allows various types of layered overriding (ex.: general defaults, controller defaults, action defaults, particular case defaults).
How to do it in PHP
You don't need to create a language parser as @geomagas said. Using include
or require
is enought, the PHP interpreter will load the "new" class into memory and ensure it is available.
All you need to do is create a template file in which to replace some values such as:
<?php
class WebConfig {
public static $WEBPATH = '$_replace_webpath';
public static $ACCOUNTPATH = '$_replace_accountpath';
public static $INFO = '$_replace_info';
const ACCOUNT_STATUS = '$_replace_account_status';
const ENABLE_SEARCH = '$_replace_enable_search';
}
And then load read the file, and replace it with the current values such as:
$config_template = file_get_contents('/path/to/config/template.php.template');
str_replace(
array('$_replace_webpath' ... ),
array('/customers' ... ),
$config_template
);
PrestaShop
uses PHP files for configuration. It rewrites them when needed.