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']= '';

多数据库主从配置为例

 

你可能感兴趣的:(配置文件)