Zend Framework Introductory Tutorial Zend_config component Usage _php examples

Source: Internet
Author: User
Tags webhost zend zend framework

This example describes the usage of Zend_config components in the Zend Framework. Share to everyone for your reference, specific as follows:

1. Reading data from the PHP array

Using Zend_config_ini (read Ini configuration file)
Zend_config_xml (read Xml configuration file)

Case:

<?php
require_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 ' => ' 123 ',
    ' db_name ' => ' Test '
    )
  ;
$config = new Zend_config ($array);
echo "Web server address is:";
echo $config->webhost;
echo "<p>";
echo "Database server name is:";
echo $config->database->db_host;
echo "<p>";
echo "Database user name is:";
echo $config->database->db_user;
echo "<p>";
echo "Database password is:";
echo $config->database->db_pass;
echo "<p>";
echo "Database name is:";
echo $config->database->db_name;
echo "<p>";

Results:

The Web server address is: 127.0.0.1
The database server name is: localhost
Database user name is: root
The database password is: 123
The database name is: Test

Description

When initializing the Zend_config class, the data in the array is changed directly to the property value of the $config object.

2. Reading data from the PHP configuration file

Case:

(1) First is the configuration file, the code is as follows.

<?php return
Array ('
  webhost ' => ' 127.0.0.1 ',
  ' database ' => array (
    ' db_host ' => ') localhost ',
    ' Db_user ' => ' root ',
    ' db_pass ' => ' 123 ',
    ' db_name ' => ' Test '
    )
;

(2) Create a read configuration of the PHP file, the code is as follows.

<?php
require_once ("zend/loader.php");
Zend_loader::loadclass (' Zend_config ');
$filename = ' config.php ';            Define the profile name
$config = new Zend_config (require $filename);  Instantiate object for Class
echo "Web server address is:";
echo $config->webhost;
echo "<p>";
echo "Database server name is:";
echo $config->database->db_host;
echo "<p>";
echo "Database user name is:";
echo $config->database->db_user;
echo "<p>";
echo "Database password is:";
echo $config->database->db_pass;
echo "<p>";
echo "Database name is:";
echo $config->database->db_name;
echo "<p>";

Description: The result is the same as before, only this time the reading data is obtained from the PHP file. General needs of the configuration information, nothing more than the database name, user name, password and so on.

3. Read data from the INI file

Subclasses of the Zend_config component Zend_config_ini allow reading of data from the INI configuration file.

Case:

(1) Create an INI configuration file for the read content, as follows.

[Database]
hostname = localhost
database.type = mysql
database.host = localhost
database.user = root
Database.pass = 123
Database.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.

<?php
require_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 object for Class
echo "database server name is:";
echo $config->hostname;
echo "<p>";
echo "Database type is:";
echo $config->database->type;
echo "<p>";
echo "Database user name is:";
echo $config->database->user;
echo "<p>";
echo "Database password is:";
echo $config->database->pass;
echo "<p>";
echo "Database name is:";
echo $config->database->name;
echo "<p>";

Note: After instantiating an object, you can use the information in the configuration file by specifying the appropriate parameters.

The results are:

The database server name is: localhost
The database type is: MySQL
Database user name is: root
The database password is: 123
The database name is: Test_ini

4. Call the data read from the INI in the form of an array

Using ToArray () method to realize transformation

Case:

<?php
require_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 an object for a class
$temp = $config->database->toarray ();      Makes the data of one of the properties into an array and assigns the data to the variable $temp
echo "database type:";
echo $temp [type];
echo "<p>";
echo "Database user name is:";
echo $temp [user];
echo "<p>";
echo "Database password is:";
echo $temp [pass];
echo "<p>";
echo "Database name is:";
echo $temp [name];
echo "<p>";

Results:

The database type is: MySQL
Database user name is: root
The database password is: 123
The database name is: Test_ini

5. Reading data from an XML configuration file

Using subclass Zend_config_xml to implement
root element top-level elements
Section-level Elements Section Element
Leaf elements leaves element

Case:

(1) Create an XML file

<?xml Version = ' 1.0 '?>
<config>
  <production>
    <webhost>127.0.0.1</webhost >
    <database>
      <type>pdo_mysql</type>
       
 

Named Config.xml

(2) Set up a PHP file to read the XML file data, the code is as follows.

<?php
require_once ("zend/loader.php");
Zend_loader::loadclass (' Zend_config_xml ');
$filename = ' config.xml ';            Define the profile name
$section = "stag";                Defines the section name that needs to be loaded
$config = new Zend_config_xml ($filename, $section);  Instantiate object for Class
echo "server address is:";
echo $config->webhost;
echo "<p>";
echo "Database type is:";
echo $config->database->type;
echo "<p>";
echo "Database user name is:";
echo $config->database->username;
echo "<p>";
echo "Database password is:";
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

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.