2

I have a config file which is written in PHP as below,

<?php

class WebConfig {

    public static $WEBPATH = '/customers';
    public static $ACCOUNTPATH = '/empaccountpath';
    public static $INFO = '/accountInfo';
    const ACCOUNT_STATUS = '/account_status';
    const ENABLE_SEARCH = '/enable_search';
}

?>

So I would like to develop an interface in PHP, which has ability to edit the file values such as $WEBPATH, $ACCOUNTPATH and const values.

Simple PHP editor script will do the above work. But it doesn't check the syntax.

Please suggest how to do it in PHP efficiently.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
Raja
  • 3,477
  • 12
  • 47
  • 89
  • 2
    Reinvent the wheel by coding a php parser in php. Or, you could save your config parameters in a DB/textfile and do it the "naive" way... ;) – geomagas Nov 05 '13 at 07:46
  • 1
    Additionally, you can create different files for different environment and include them dynamically as per environment in which they are running. – Suresh Kamrushi Nov 05 '13 at 08:09
  • Store it as an ini file (in text) - there's a PHP command to parse this, the results of which can be checked in PHP. – halfer Nov 05 '13 at 08:09
  • Create a script that generates the php file from a list of values. – JAL Nov 05 '13 at 08:27

2 Answers2

3

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.

Community
  • 1
  • 1
Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
0

Directly to access the PHP Class file too danger....Many security issues....

Could we use simple html form to let user to edit.

Using JSON format to store into file.

PHP encode & decode by json_encode() & json_decode(), when save & read the value.

Allen Chak
  • 1,802
  • 1
  • 10
  • 21
  • I see no security issues if the class was not generated using user-driven or user-supplied data. And even if it were using user-supplied data it would be pretty simple to both protect the admin area from outsiders (trusting the user) as well as validate his settings against a list of known allowed values. – Mihai Stancu Nov 05 '13 at 08:27