哥们,用zendConfig 来解决这些超快超方便.
关于数组的
// Given an array of configuration data
$configArray = array(
'webhost' => 'www.example.com',
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
);
require_once 'Zend/Config.php';
$config = new Zend_Config($configArray);
echo $config->webhost;
关于ini的
require_once 'Zend/Config/Ini.php';
$config = new Zend_Config_Ini('/path/to/config.ini', 'staging');
echo $config->database->params->host; // prints "dev.example.com"
echo $config->database->params->dbname; // prints "dbname"关于xml的.
<?xml version="1.0"?>
<configdata>
<production>
<webhost>www.example.com</webhost>
<database>
<adapter>pdo_mysql</adapter>
<params>
<host>db.example.com</host>
<username>dbuser</username>
<password>secret</password>
<dbname>dbname</dbname>
</params>
</database>
</production>
<staging extends="production">
<database>
<params>
<host>dev.example.com</host>
<username>devuser</username>
<password>devsecret</password>
</params>
</database>
</staging>
</configdata>
Next, assume that the application developer needs the staging configuration data from the XML file. It is a simple matter to load these data by specifying the XML file and the staging section: <?php
require_once 'Zend/Config/Xml.php';$config = new Zend_Config_Xml('/path/to/config.xml', 'staging');echo $config->database->params->host; // prints "dev.example.com"
echo $config->database->params->dbname; // prints "dbname"
关于数组的
// Given an array of configuration data
$configArray = array(
'webhost' => 'www.example.com',
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
);
require_once 'Zend/Config.php';
$config = new Zend_Config($configArray);
echo $config->webhost;
关于ini的
require_once 'Zend/Config/Ini.php';
$config = new Zend_Config_Ini('/path/to/config.ini', 'staging');
echo $config->database->params->host; // prints "dev.example.com"
echo $config->database->params->dbname; // prints "dbname"关于xml的.
<?xml version="1.0"?>
<configdata>
<production>
<webhost>www.example.com</webhost>
<database>
<adapter>pdo_mysql</adapter>
<params>
<host>db.example.com</host>
<username>dbuser</username>
<password>secret</password>
<dbname>dbname</dbname>
</params>
</database>
</production>
<staging extends="production">
<database>
<params>
<host>dev.example.com</host>
<username>devuser</username>
<password>devsecret</password>
</params>
</database>
</staging>
</configdata>
Next, assume that the application developer needs the staging configuration data from the XML file. It is a simple matter to load these data by specifying the XML file and the staging section: <?php
require_once 'Zend/Config/Xml.php';$config = new Zend_Config_Xml('/path/to/config.xml', 'staging');echo $config->database->params->host; // prints "dev.example.com"
echo $config->database->params->dbname; // prints "dbname"
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货