Introduction: This is a detailed page for sharing a self-written PHP config class. It introduces PHP, PHP, config, code to share a self-written PHP config class related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 344673 'rolling = 'no'>
The biggest feature of this class is that it can load wireless depth configuration items, and some configuration items can be modified during the configuration process, with the depth not more than 5 levels.
/*** Config. PHP ** discription ** @ filename config. PHP * @ version V1.0 * @ update 2011-8-9 * @ author Randy. hong * @ Contact homingway@163.com * @ package config */define ('ds', directory_separator); define ('path _ config ','. '. DS. 'configs'); // config Param key separatordefine ('config _ separator ','. '); Class config {protected static $ _ configarray = array ();/*** get a configuration * @ Param string $ key * @ return mixed */P Ublic static function get ($ key = '') {// inlegal Param, return falseif (! $ Key) {return false;} // without Separator in Param, return the whole config fileif (strpos ($ key, config_separator) === false) {If (! Isset (Self ::$ _ configarray [$ key]) {$ pai_file = path_config.ds. 'config. '. $ key. '. PHP '; If (file_exists ($ pai_file) {self ::: _ configarray [$ key] = include_once ($ pai_file) ;}} return self :: $ _ configarray [$ key];} else {$ Param = explode (config_separator, $ key); If (! Isset (Self ::$ _ configarray [$ Param [0]) {$ pai_file = path_config.ds. 'config. '. $ Param [0]. '. PHP '; If (file_exists ($ pai_file) {self ::: _ configarray [$ Param [0] = include_once ($ pai_file); }}$ tmp_config = NULL; for ($ I = 1; $ I <count ($ PARAM); $ I ++) {if ($ I = 1) {If (isset (SELF :: $ _ configarray [$ Param [0] [$ Param [1]) {$ tmp_config = self :: $ _ configarray [$ Param [0] [$ Param [1];} else {return false ;}} else {If (isset ($ tmp_config [$ Param [$ I]) {$ tmp_config = $ tmp_config [$ Param [$ I];} else {return false ;}} return $ tmp_config ;}} /*** change the value of a configuration item * @ Param string $ key * @ Param mixed $ value * @ return true */public static function set ($ key, $ value) {$ Param = explode (config_separator, $ key); $ count_param = count ($ PARAM); Switch ($ count_param) {Case 1: Self :: $ _ configarray [$ Param [0] = $ value; break; Case 2: Self: $ _ configarray [$ Param [0] [$ Param [1] = $ value; break; Case 3: Self :: $ _ configarray [$ Param [0] [$ Param [1] [$ Param [2] = $ value; break; Case 4: Self :: $ _ configarray [$ Param [0] [$ Param [1] [$ Param [2] [$ Param [3] = $ value; break; Case 5: SELF :: $ _ configarray [$ Param [0] [$ Param [1] [$ Param [2] [$ Param [3] [$ Param [4] = $ value; break; default: break;} return true ;}}?>
Configuration File: configs/config. Test. php
Return array ('test1' => array ('test2' => array ('test3' => array ('test4' => array ('test5' => 5555 ,), ),),));
Call File
Include_once ('config. PHP '); $ Config = config: Get ('test. test1 '); print_r ($ config); config: Set ('test. test1', 222); $ Config = config: Get ('test. test1 '); print_r ($ config );
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/344673.html pageno: 4.