温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php自定义配置文件的读取

发布时间:2020-07-15 19:23:03 来源:网络 阅读:666 作者:tinytail 栏目:web开发

自定义配置文件config.php

return array(

       'c_type' => 1,

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

)

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

 

/**
    *  配置文件数组值的获取
    *  $host = Util_Tool::config('database.default.connection.hostname')
    *  @param  $str string
    *  @param  array
    *
    */
   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;
       }
   }
   /**
    * Gets a value from an array using a dot separated path.
    *
    *     // Get the value of $array['foo']['bar']
    *     $value = Util_Tool::path($array, 'foo.bar');
    *
    * Using a wildcard "*" will search intermediate arrays and return an array.
    *
    *     // Get the values of "color" in theme
    *     $colors = Util_Tool::path($array, 'theme.*.color');
    *
    *     // Using an array of keys
    *     $colors = Util_Tool::path($array, array('theme', '*', 'color'));
    *
    * @param   array   array to search
    * @param   mixed   key path string (delimiter separated) or array of keys
    * @param   mixed   default value if the path is not set
    * @param   string  key path delimiter
    * @return  mixed
    */
   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;
   }



/**
    *  获取配置文件的值
    *  @copyright liwan 2013-10-06
    *  @param $key 标识config文件名 配置文件里统一使用return array的形式
    */
   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];
   }

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI