This article mainly introduces the Zend_config component usage of the Zend Framework Introductory tutorial, and analyzes the related techniques of zend_config components for various types of profile operations in the form of examples, and the friends you need can refer to the following
The examples in this article describe the use of zend_config components in the Zend Framework. Share to everyone for your reference, as follows:
1. Reading data from the PHP array
Using Zend_config_ini (read Ini configuration file)
Zend_config_xml (reading Xml configuration file)
Case:
<?phprequire_once ("zend/loader.php"); Zend_loader::loadclass (' Zend_config '); $array = Array ( ' webhost ' = ' 127.0.0.1 ', ' database ' =>array ( ' db_host ' + ' localhost ', ' db_user ' + ' root ', ' db_pass ' and ' 123 ', ' db_name ' = ' test ' ) ); $config = new Zend_config ($array); echo "Web server address:"; Echo $config->webhost;echo "<p>"; echo "Database server name:"; echo $ Config->database->db_host;echo "<p>"; echo "Database user name:"; Echo $config->database->db_user;echo "<p > "; echo" Database password is: "Echo $config->database->db_pass;echo" <p> "; echo" database name: "; Echo $config Database->db_name;echo "<p>";
Results:
The Web server address is: 127.0.0.1
Database server name is: localhost
The database user name is: root
Database password is: 123
The database name is: Test
Description
When initializing the Zend_config class, the data in the array is changed directly into the property values of the $config object.
2. Reading data from a PHP configuration file
Case:
(1) First is the configuration file, the code is as follows.
<?phpreturn Array ( ' webhost ' = ' 127.0.0.1 ', ' database ' = = Array ( ' db_host ' = ' localhost ', ' db_user ' = ' root ', ' db_pass ' = ' 123 ', ' db_name ' = ' test ' );
(2) Create a PHP file to read the configuration, the code is as follows.
<?phprequire_once ("zend/loader.php"); Zend_loader::loadclass (' Zend_config '); $filename = ' config.php '; Define the profile name $config = new Zend_config (require $filename); Instantiate the object for the class echo "Web server address:"; Echo $config->webhost;echo "<p>"; echo "Database server name:"; Echo $config->database- >db_host;echo "<p>"; echo "Database user name:", echo $config->database->db_user;echo "<p>"; echo "Database password:"; echo $config->database->db_pass;echo "<p>", echo "Database name:", Echo $config->database->db_name;echo " <p> ";
Description: The result is the same as before, except that the read data is obtained from the PHP file. Generally required configuration information, is nothing more than the database name, user name, password and so on.
3. Reading data from the INI file
The subclass of the Zend_config component Zend_config_ini allows data to be read from the INI configuration file.
Case:
(1) Create an INI configuration file for the content to be read, the code is as follows.
[Database]hostname = Localhostdatabase.type = Mysqldatabase.host = Localhostdatabase.user = Rootdatabase.pass = 123database.name = Test_ini
Save As Config.ini
(2) Create a PHP file to read the contents of the configuration file, the code is as follows.
<?phprequire_once ("zend/loader.php"); Zend_loader::loadclass (' Zend_config_ini '); $filename = ' config.ini '; Define the profile name $config = new Zend_config_ini ($filename, ' database '); Instantiate the object for the class echo "Database server name:"; Echo $config->hostname;echo "<p>", echo "Database type:", echo $config->database-> Type;echo "<p>"; echo "Database user name:", echo $config->database->user;echo "<p>"; echo "Database password:"; echo $ Config->database->pass;echo "<p>"; echo "database name:"; Echo $config->database->name;echo "<p>";
Description: After instantiating an object, you can use the information in the configuration file by specifying the appropriate parameters.
The result is:
Database server name is: localhost
The database type is: MySQL
The database user name is: root
Database password is: 123
The database name is: Test_ini
4. Call the data read in the INI as an array
Using the ToArray () method to achieve conversions
Case:
<?phprequire_once ("zend/loader.php"); Zend_loader::loadclass (' Zend_config_ini '); $filename = ' config.ini '; Define the profile name $config = new Zend_config_ini ($filename, ' database '); Instantiating an object for a class $temp = $config->database->toarray (); Make the data of one of the properties into an array and assign the data to the variable $tempecho "database type:"; echo $temp [Type];echo "<p>"; echo "Database user name:"; echo $temp [user]; echo "<p>", echo "Database password:", echo $temp [Pass];echo "<p>"; echo "database name:"; Echo $temp [Name];echo "<p>";
Results:
The database type is: MySQL
The database user name is: root
Database password is: 123
The database name is: Test_ini
5. Reading data from an XML configuration file
By subclass Zend_config_xml to achieve
root element top-level elements
Section-level Elements Section Element
Leaf elements Element
Case:
(1) Create an XML file
<?xml Version = ' 1.0 '?><config> <production> <webhost>127.0.0.1</webhost> <database> <type>pdo_mysql</type>
Name is CONFIG.
(2) Create a PHP file to read the XML file data, the code is as follows.
<?phprequire_once ("zend/loader.php"); Zend_loader::loadclass (' Zend_config_xml '); $filename = ' config + '; Define the profile name $section = "Stag"; Defines the section name that needs to be loaded $config = new Zend_config_xml ($filename, $section); Instantiate the object for the class echo "server address:"; Echo $config->webhost;echo "<p>", echo "Database type:", echo $config->database->type echo "<p>", echo "Database user name:", echo $config->database->username;echo "<p>"; echo "Database password:"; echo $ Config->database->password;echo "<p>";
Results:
The server address is: 127.0.0.1
The database type is: Pdo_mysql
Database user name is: db_user
The database password is: Db_pass
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!