需要建议通过php编辑php

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.

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.

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.