zoukankan      html  css  js  c++  java
  • php读取文件里面的数组做为配置文件

    可能大家也都见过很多开源的产品,大多它们的配置文件都存放在一个单独的文件中,而这个文件里只存放了一个数组,其实这里运用了一个PHP的小技巧,就是可以将文件包含进来,并且赋值给一个变量,这个变量就具有了整个配置的数组,我给大家举个例子:
    
    config.php 
    --------------------------
    <?php
    return array(
                'a' => 1,
                'b' => 2,
                'c'  => 3,
    );
    ?>
    
    getconfig.php
    --------------------------
    <?php
    $config = require('config.php');
    
    print_r($config);
    ?>
    
    大家试一下,会发现$config变量里存储了config.php中的数组, 这就是运用了php可以把文件当作return函数的一种用法。
    这种类似于ci的配置文件处理的方法

    config.php -------------------------- <?php

    $config['ss'] = 100;
    $config['sess'] = 200;
    $config['sess_update'] = 300;

    ?>
    
    getconfig.php
    --------------------------
    <?php
    require('config.php');
    
    print_r($config);
    return array(
               'master'=>array(
                    'master_host'=>'1',
                    'master_name'=>'1',
                    'master_password'=>'1',
                    'master_port'=>'1',
                    'slave1'=>array(
                        'slave_host'=>'2',
                        'slave_name'=>'2',
                        'slave_password'=>'2',
                        'slave_port'=>'2',
                    ),
                      'slave2'=>array(
                        'slave_host'=>'3',
                        'slave_name'=>'3',
                        'slave_password'=>'3',
                        'slave_port'=>'3',
                    ),
               ),
               'master1'=>array(
                    'master_host'=>'5',
                    'master_name'=>'5',
                    'master_password'=>'5',
                    'master_port'=>'5',
                    'slave1'=>array(
                        'slave_host'=>'6',
                        'slave_name'=>'6',
                        'slave_password'=>'6',
                        'slave_port'=>'6',
                    ),
                      'slave2'=>array(
                        'slave_host'=>'7',
                        'slave_name'=>'7',
                        'slave_password'=>'7',
                        'slave_port'=>'7',
                    ),
               ),
    
    );
    
    
    
    $config['db']['master']['host']= '';
    $config['db']['master']['name']= '';
    $config['db']['master']['password']= '';
    $config['db']['master']['port']= '';
    $config['db']['master']['slave1']['host']= '';
    $config['db']['master']['slave1']['name']= '';
    $config['db']['master']['slave1']['password']= '';
    $config['db']['master']['slave1']['port']= '';
    $config['db']['master']['slave2']['host']= '';
    $config['db']['master']['slave2']['name']= '';
    $config['db']['master']['slave2']['password']= '';
    $config['db']['master']['slave2']['port']= '';
    
    $config['db']['master1']['host']= '';
    $config['db']['master1']['name']= '';
    $config['db']['master1']['password']= '';
    $config['db']['master1']['port']= '';
    $config['db']['master1']['slave1']['host']= '';
    $config['db']['master1']['slave1']['name']= '';
    $config['db']['master1']['slave1']['password']= '';
    $config['db']['master1']['slave1']['port']= '';
    $config['db']['master1']['slave2']['host']= '';
    $config['db']['master1']['slave2']['name']= '';
    $config['db']['master1']['slave2']['password']= '';
    $config['db']['master1']['slave2']['port']= '';

    多数据库主从配置为例
  • 相关阅读:
    h.264宏块与子宏块类型
    h.264语法结构分析
    [傅里叶变换及其应用学习笔记] 关于任何信号都能表现成傅里叶级数形式的推导
    [傅里叶变换及其应用学习笔记] 课程概览
    [傅里叶变换及其应用学习笔记] 三十. 拉东变换
    [傅里叶变换及其应用学习笔记] 二十九. 高维Ш函数修改版
    后端解决 微信H5支付 商户参数格式错误 方法
    PhpStorm一次性折叠所有函数或者方法
    js生成的cookie在yii2中获取不到的解决办法
    Android webview 调起H5微信支付
  • 原文地址:https://www.cnblogs.com/zx-admin/p/4383786.html
Copyright © 2011-2022 走看看