栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

php自定义配置文件的读取

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

php自定义配置文件的读取

自定义配置文件config.php

return array(

       'c_type' => 1,

       'spage'=>array(
          't'=>2,
       ),

)

访问形式 Util_Tool::config('config.spage.t') 获取t的值

 


   public static function config($group){
       static $config;

       if (strpos($group, '.') !== FALSE)
       {
           // Split the config group and path
           list ($group, $path) = explode('.', $group, 2);
       }
       //return $group;
       $config =  Zwp_Config::get_config($group);

       if ( ! isset($config))
       {
           // Load the config group into the cache
           $config = array();
       }

       if (isset($path))
       {
           return self::path($config, $path, NULL, '.');
       }
       else
       {
           return $config;
       }
   }
   
   public static function path($array, $path, $default = NULL, $delimiter = NULL)
   {
       if (!is_array($array))
       {
           // This is not an array!
           return $default;
       }

       if (is_array($path))
       {
           // The path has already been separated into keys
           $keys = $path;
       }
       else
       {
           if (array_key_exists($path, $array))
           {
               // No need to do extra processing
               return $array[$path];
           }

           if ($delimiter === NULL)
           {
               // Use the default delimiter
               $delimiter = '.';
           }

           // Remove starting delimiters and spaces
           $path = ltrim($path, "{$delimiter} ");

           // Remove ending delimiters, spaces, and wildcards
           $path = rtrim($path, "{$delimiter} *");

           // Split the keys by delimiter
           $keys = explode($delimiter, $path);
       }

       do
       {
           $key = array_shift($keys);

           if (ctype_digit($key))
           {
               // Make the key an integer
               $key = (int) $key;
           }

           if (isset($array[$key]))
           {
               if ($keys)
               {
                   if (is_array($array[$key]))
                   {
                       // Dig down into the next part of the path
                       $array = $array[$key];
                   }
                   else
                   {
                       // Unable to dig deeper
                       break;
                   }
               }
               else
               {
                   // Found the path requested
                   return $array[$key];
               }
           }
           elseif ($key === '*')
           {
               // Handle wildcards

               $values = array();
               foreach ($array as $arr)
               {
                   if ($value = self::path($arr, implode('.', $keys)))
                   {
                       $values[] = $value;
                   }
               }

               if ($values)
               {
                   // Found the values requested
                   return $values;
               }
               else
               {
                   // Unable to dig deeper
                   break;
               }
           }
           else
           {
               // Unable to dig deeper
               break;
           }
       }
       while ($keys);

       // Unable to find the value requested
       return $default;
   }




   public static function get_config($key)
   {
       static $new_config;
       if (isset($new_config[$key])) return $new_config[$key];
       if (is_file(CONFIG_DIR . '/' . $key . '.php')) {
           $new_config[$key] = require CONFIG_DIR . '/' . $key . '.php';
       } else return false;
       return $new_config[$key];
   }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/227461.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号