In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "which read and write ini format PHP configuration classes", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "which read and write ini format PHP configuration classes" bar!
Basically meet all configuration-related requirements.
/ * * parse the configuration file in .ini format into a tree-structured object * different section inherits through colons * the section used is determined by default based on hostname, and if you are not sure, use production first * always give priority to detecting production when detecting the environment The rest of the section detects * * @ author ares@phpdr.net * * / class Config {/ * parsed configuration files * * @ var stdClass * / private $config in defined order / * A two-dimensional array, the key is the section of the configuration file * the value is an array or callback function * if it is an array, calculate whether the hostname decides whether to use the section in the array * if it is a callback function, determine whether to use the section * * @ var array * / private $map = array () by returning the value true or false; / * * section will be parsed, indicating inheritance of'.'in the configuration item. Used to distinguish between'.'in a hierarchical relationship * section It will not be parsed and the array in the configuration will not be affected. * * @ param string $conf * @ throws ErrorException * @ return stdClass * / function _ construct ($conf, $map) {$config = $this- > parseIni ((object) parse_ini_string ($conf, true)); if (array_key_exists ('production', $map)) {$production = $map [' production']; unset ($map ['production']); $map = array_merge (array (' production' = > $production), $map) } else {throw new ErrorException ('production section not found in config');} $section =' production'; $hostname = gethostname (); foreach ($map as $k = > $v) {if (is_array ($v)) {foreach ($v as $v1) {if ($v1 = = $hostname) {$section = $k; break 2;} elseif (is_callable ($v)) {if (true = call_user_func ($v)) {$section = $k break; }} else {throw new ErrorException ('Wrong map value in'. _ _ CLASS__);} $this- > config = $config- > $section;} / * always returns the configuration object * * @ return mixed * / function _ _ get ($key) {if ($this- > config- > $key) {return $this- > config- > $key }} / * param stdClass $v * @ param string $K1 * @ param mixed $v1 * / private function split ($v, $K1, $v1) {$keys = explode ('.', $K1); $last = array_pop ($keys); $node = $v; foreach ($keys as $v2) {if (! Isset ($node- > $v2) {$node- > $v2 = new stdClass ();} $node = $node- > $v2;} $node- > $last = $v1; if (count ($keys) > 0) {unset ($v-> $K1);}} / * * parseIni * * @ param object $conf * @ return stdClass * / private function parseIni ($conf) {$confObj = new stdClass () Foreach ($conf as $k = > $v) {/ / is section if (is_array (v)) {$confObj- > $k = (object) $v; foreach ($v as $K1 = > $v1) {call_user_func (array ($this, 'split'), $confObj- > $k, $v1);}} else {call_user_func (array ($this,' split'), $confObj, $k, $v);} unset ($conf) / / process inheritance foreach ($confObj as $k = > $v) {if (false! = = strpos ($k,':')) {if (0 = strpos ($k,':')) {throw new ErrorException ('config'). $k. ' Is invalid,': 'can't be the first char');} elseif (1
< substr_count ( $k, ':' )) { throw new ErrorException ( 'config ' . $k . ' is invalid, ':' can appear only once' ); } else { $keys = explode ( ':', $k ); if (! isset ( $confObj->$keys [1]) {throw new ErrorException ('parent section'. $keys [1]. ' Doesn't exist in config file');} else {if (isset ($confObj- > $keys [0])) {throw new ErrorException ('config is invalid,'. $keys [0]. ' And'. $k. ' Conflicts');} else {$confObj- > $keys [0] = $this- > deepCloneR ($confObj- > $keys [1]); $this- > objectMergeR ($confObj- > $keys [0], $v); unset ($confObj- > $k);}} return $confObj;} / * * php is shallow cloning by default, and deep cloning of functions is achieved * * @ param object $obj * @ return object $obj * / private function deepCloneR ($obj) {$objClone = clone $obj Foreach ($objClone as $k = > $v) {if (is_object ($v)) {$objClone- > $k = $this- > deepCloneR ($v);}} return $objClone } / * Recursively merge two objects * * @ param unknown $obj1 * @ param unknown $obj2 * / private function objectMergeR ($obj1, $obj2) {foreach ($obj2 as $k = > $v) {if (is_object ($v) & & isset ($obj1- > $k) & & is_object ($obj1- > $k)) {$this- > objectMergeR ($obj1- > $k, $v);} else {$obj1- > $k = $v;}
Easy to use:
$_ ENV ['config'] = new Config (file_get_contents (_ _ DIR__. '/ config.ini'), array (' development' = > array ('localhost.localdomain',' localhost'), 'production' = > array ()
Example of a profile:
[product] db.default.dsn= "mysql:host=127.0.0.1;dbname=default" db.default.username=rootdb.default.password=123456admin.username=adminadmin.password=123456php.error_reporting=E_ALLphp.display_errors=nophp.log_errors=yesphp.error_log=APP_PATH'/resource/log/phpError.log'php.session.save_path=APP_PATH'/resource/data/session' [development:product] db.test1.dsn= "mysql:host=127.0.0.1 Dbname=test1 "db.test1.username=rootdb.test1.password=123456php.display_errors=yes thank you for reading, the above is the content of" read and write PHP configuration classes in ini format ". After the study of this article, I believe you have a deeper understanding of what to read and write PHP configuration classes in ini format, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.