This article mainly introduces how to dynamically modify the configuration file of PHP, has a certain reference value, now share to everyone, the need for friends can refer to
A dynamic site, generally have a variety of background configuration, if the configuration is not much, design a table alone seems to be a waste of resources
Some children's shoes like to have a variety of configuration directly stored in the project, want to do the background can be controlled, you need a set of configuration files to make additions and deletions to change the operation method
Don't say nonsense directly let us, da ~ da ~ da ~
Create a new PHP file, name it Config.class.php, and then follow the comments to use the line ~
/* * @link https://mkblog.cn/* @author Mengkun * @license MIT *//** * PHP No database configuration file additions/deletions Check module *! Note: This module is not optimized for high concurrency, is it better to use a database if the amount of data or concurrency is too large? * * How to use: * * First, refer to this module * * require_once ' Config.class.php '; * * * Second, initialize * * $C = new Config (' config file name '); * If it is in the level two directory, make sure that the directory exists * * * Three, built-in method * *-storage (if already exists is modified) Single data * * $C->set (' sitename ', ' da da '); * * *-Storage (if existing then is modified) an array * * $C->set (' user ', Array (' name ' = ' Peter ', ' Age ' =>12)); * * *-Read a data * * $C->get (' user ', ' default value '); * * *-delete a piece of data * * $C->delete (' user '); * * *-Save changes to data * * $C->save (); The save succeeds returns true, otherwise the reason for the failure is returned * * * * NOTE: In order to avoid writing files frequently, all operations on the data must be called once $C->save (); is actually saved to the configuration file * It is recommended that all data operations be performed before the storage operation is completed. * * * Attached: simplified wording * * $C->set (' sitename ', ' da da ')->save (); */define (' Config_exit ', ' <?php EXIT;? > '); Class Config {private $data; Private $file; /** * constructor * @param $fIle Storage Data File * @return */function __construct ($file) {$file = $file. '. PHP '; $this->file = $file; $this->data= Self::read ($file); /** * Read Config file * @param $file data file to read * All data information Read @return * * Public Function Read ($file) { if (!file_exists ($file)) return array (); $str = file_get_contents ($file); $str = substr ($str, strlen (config_exit)); $data = Json_decode ($str, true); if (Is_null ($data)) return array (); return $data; }/** * Gets the value of the specified item * @param $key the item name to get * @param $default Default * @return Data * * Public fun Ction get ($key = null, $default = ") {if (Is_null ($key)) return $this->data; Take all data if (Isset ($this->data[$key])) return $this->data[$key]; return $default; }/** * Sets the value of the specified item * @param $key the item name to set * @param $value value * @return NULL */PubliC function Set ($key, $value) {if (is_string ($key)) {//Update single data $this->data[$key] = $value; } else if (Is_array ($key)) {//Update multiple data foreach ($this->data as $k + = $v) { if ($v [$key [0]] = = $key [1]) {$this->data[$k] [$value [0]] = $value [1]; }}} return $this; /** * Delete and empty the specified item * @param $key DELETE key name * @return NULL */Public Function Delete ($key) { unset ($this->data[$key]); return $this; /** * Save config file * @param $file data file to save * @return true-Success Other-save failed reason */Public function Save () {if (defined (' Json_pretty_print ')) {$jsonStr = Json_encode ($this->data, json_unescaped_unicode| Json_pretty_print); } else {$jsonStr = Json_encode ($this->data); }//contains binary or non-UTF8 string correspondence detection if (Is_null ($JSONSTR)) return ' data file error '; $buffer = Config_exit. $jsonStr; $file _STRM = fopen ($this->file, ' W '); if (! $file _strm) Return ' Write file failed, give '. $file. ' File Write permission! '; Fwrite ($file _strm, $buffer); Fclose ($file _STRM); return true; } }
Code Capture Source Mengken Blog
A dynamic site, generally have a variety of background configuration, if the configuration is not much, design a table alone seems to be a waste of resources
Some children's shoes like to have a variety of configuration directly stored in the project, want to do the background can be controlled, you need a set of configuration files to make additions and deletions to change the operation method
Don't say nonsense directly let us, da ~ da ~ da ~
Create a new PHP file, name it Config.class.php, and then follow the comments to use the line ~
/* * @link https://mkblog.cn/* @author Mengkun * @license MIT *//** * PHP No database configuration file additions/deletions Check module *! Note: This module is not optimized for high concurrency, is it better to use a database if the amount of data or concurrency is too large? * * How to use: * * First, refer to this module * * require_once ' Config.class.php '; * * * Second, initialize * * $C = new Config (' config file name '); * If it is in the level two directory, make sure that the directory exists * * * Three, built-in method * *-storage (if already exists is modified) Single data * * $C->set (' sitename ', ' da da '); * * *-Storage (if existing then is modified) an array * * $C->set (' user ', Array (' name ' = ' Peter ', ' Age ' =>12)); * * *-Read a data * * $C->get (' user ', ' default value '); * * *-delete a piece of data * * $C->delete (' user '); * * *-Save changes to data * * $C->save (); The save succeeds returns true, otherwise the reason for the failure is returned * * * * NOTE: In order to avoid writing files frequently, all operations on the data must be called once $C->save (); is actually saved to the configuration file * It is recommended that all data operations be performed before the storage operation is completed. * * * Attached: simplified wording * * $C->set (' sitename ', ' da da ')->save (); */define (' Config_exit ', ' <?php EXIT;? > '); Class Config {private $data; Private $file; /** * constructor * @param $fIle Storage Data File * @return */function __construct ($file) {$file = $file. '. PHP '; $this->file = $file; $this->data= Self::read ($file); /** * Read Config file * @param $file data file to read * All data information Read @return * * Public Function Read ($file) { if (!file_exists ($file)) return array (); $str = file_get_contents ($file); $str = substr ($str, strlen (config_exit)); $data = Json_decode ($str, true); if (Is_null ($data)) return array (); return $data; }/** * Gets the value of the specified item * @param $key the item name to get * @param $default Default * @return Data * * Public fun Ction get ($key = null, $default = ") {if (Is_null ($key)) return $this->data; Take all data if (Isset ($this->data[$key])) return $this->data[$key]; return $default; }/** * Sets the value of the specified item * @param $key the item name to set * @param $value value * @return NULL */PubliC function Set ($key, $value) {if (is_string ($key)) {//Update single data $this->data[$key] = $value; } else if (Is_array ($key)) {//Update multiple data foreach ($this->data as $k + = $v) { if ($v [$key [0]] = = $key [1]) {$this->data[$k] [$value [0]] = $value [1]; }}} return $this; /** * Delete and empty the specified item * @param $key DELETE key name * @return NULL */Public Function Delete ($key) { unset ($this->data[$key]); return $this; /** * Save config file * @param $file data file to save * @return true-Success Other-save failed reason */Public function Save () {if (defined (' Json_pretty_print ')) {$jsonStr = Json_encode ($this->data, json_unescaped_unicode| Json_pretty_print); } else {$jsonStr = Json_encode ($this->data); }//contains binary or non-UTF8 string correspondence detection if (Is_null ($JSONSTR)) return ' data file error '; $buffer = Config_exit. $jsonStr; $file _STRM = fopen ($this->file, ' W '); if (! $file _strm) Return ' Write file failed, give '. $file. ' File Write permission! '; Fwrite ($file _strm, $buffer); Fclose ($file _STRM); return true; } }
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!