Home Backend Development PHP Tutorial About the usage of Zend_Config component in Zend Framework

About the usage of Zend_Config component in Zend Framework

Jun 15, 2018 pm 05:13 PM
framework zend

This article mainly introduces the usage of the Zend_Config component in the Zend Framework introductory tutorial, and analyzes the related techniques of the Zend_Config component for various types of configuration files in the form of examples. Friends in need can refer to the examples of this article

Describes the usage of Zend_Config component in Zend Framework. Share it with everyone for your reference, as follows:

1. Read data from PHP array

Use Zend_Config_Ini (read ini configuration file)
Zend_Config_Xml (Read XML configuration file)

Case:

<?php
require_once("Zend/Loader.php");
Zend_Loader::loadClass(&#39;Zend_Config&#39;);
$array = array(
  &#39;webhost&#39; => &#39;127.0.0.1&#39;,
  &#39;database&#39;=>array(
    &#39;db_host&#39;=>&#39;localhost&#39;,
    &#39;db_user&#39;=>&#39;root&#39;,
    &#39;db_pass&#39;=>&#39;123&#39;,
    &#39;db_name&#39;=>&#39;test&#39;
    )
  );
$config = new Zend_Config($array);
echo "Web服务器地址为:";
echo $config->webhost;
echo "<p>";
echo "数据库服务器名称为:";
echo $config->database->db_host;
echo "<p>";
echo "数据库用户名为:";
echo $config->database->db_user;
echo "<p>";
echo "数据库密码为:";
echo $config->database->db_pass;
echo "<p>";
echo "数据库名称为:";
echo $config->database->db_name;
echo "<p>";
Copy after login

Result:

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

Description:

When initializing the Zend_Config class, add it to the array The data directly becomes the attribute value of the $config object.

2. Read data from the PHP configuration file

Case:

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

<?php
return array(
  &#39;webhost&#39; => &#39;127.0.0.1&#39;,
  &#39;database&#39; => array(
    &#39;db_host&#39; => &#39;localhost&#39;,
    &#39;db_user&#39; => &#39;root&#39;,
    &#39;db_pass&#39; => &#39;123&#39;,
    &#39;db_name&#39; => &#39;test&#39;
    )
);
Copy after login

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

<?php
require_once("Zend/Loader.php");
Zend_Loader::loadClass(&#39;Zend_Config&#39;);
$filename = &#39;config.php&#39;;            //定义配置文件名
$config = new Zend_Config(require $filename);  //为类实例化对象
echo "Web服务器地址为:";
echo $config->webhost;
echo "<p>";
echo "数据库服务器名称为:";
echo $config->database->db_host;
echo "<p>";
echo "数据库用户名为:";
echo $config->database->db_user;
echo "<p>";
echo "数据库密码为:";
echo $config->database->db_pass;
echo "<p>";
echo "数据库名称为:";
echo $config->database->db_name;
echo "<p>";
Copy after login

Note: The result is the same as before, except that this time the read data is obtained from the php file. The generally required configuration information is nothing more than database name, user name, password, etc.

3. Read data from the ini file

The subclass Zend_Config_Ini of the Zend_Config component allows reading data 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 = localhost
database.type = mysql
database.host = localhost
database.user = root
database.pass = 123
database.name = test_ini
Copy after login

Save as config.ini

(2) Create a PHP file that reads the contents of the configuration file. The code is as follows.

<?php
require_once("Zend/Loader.php");
Zend_Loader::loadClass(&#39;Zend_Config_Ini&#39;);
$filename = &#39;config.ini&#39;;            //定义配置文件名
$config = new Zend_Config_Ini($filename,&#39;database&#39;);  //为类实例化对象
echo "数据库服务器名称为:";
echo $config->hostname;
echo "<p>";
echo "数据库类型为:";
echo $config->database->type;
echo "<p>";
echo "数据库用户名为:";
echo $config->database->user;
echo "<p>";
echo "数据库密码为:";
echo $config->database->pass;
echo "<p>";
echo "数据库名称为:";
echo $config->database->name;
echo "<p>";
Copy after login

Note: After instantiating the object and specifying the corresponding parameters, you can use the information in the configuration file.

The result is:

The database server name is: localhost
The database type is: mysql
The database user name is: root
The database password is: 123
The database name is: test_ini

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

Use toArray() method to achieve conversion

Case:

<?php
require_once("Zend/Loader.php");
Zend_Loader::loadClass(&#39;Zend_Config_Ini&#39;);
$filename = &#39;config.ini&#39;;            //定义配置文件名
$config = new Zend_Config_Ini($filename,&#39;database&#39;);  //为类实例化对象
$temp = $config->database->toArray();      //使得其中一个属性的数据转为数组,并将数据赋给变量$temp
echo "数据库类型为:";
echo $temp[type];
echo "<p>";
echo "数据库用户名为:";
echo $temp[user];
echo "<p>";
echo "数据库密码为:";
echo $temp[pass];
echo "<p>";
echo "数据库名称为:";
echo $temp[name];
echo "<p>";
Copy after login

Result:

The database type is: mysql
The database user name is: root
The database password is: 123
The database name is: test_ini

5. Reading data from the XML configuration file

Achieved through the subclass Zend_Config_Xml
root element top element
section-level elements section elements
leaf elements leaf elements

Case:

(1)Create an XML file

<?xml version = &#39;1.0&#39;?>
<config>
  <production>
    <webhost>127.0.0.1</webhost>
    <database>
      <type>pdo_mysql</type>
      <host>localhost</host>
      <username>db_user</username>
      <password>db_pass</password>
      <name>test</name>
    </database>
  </production>
  <stag extends="production">
    <database>
      <host>localhost</host>
      <username>db_user</username>
      <password>db_pass</password>
    </database>
  </stag>
</config>
Copy after login

named config.xml

(2) Create a PHP file that reads XML file data. The code is as follows.

<?php
require_once("Zend/Loader.php");
Zend_Loader::loadClass(&#39;Zend_Config_Xml&#39;);
$filename = &#39;config.xml&#39;;            //定义配置文件名
$section = "stag";                //定义需要加载的节名
$config = new Zend_Config_Xml($filename,$section);  //为类实例化对象
echo "服务器地址为:";
echo $config->webhost;
echo "<p>";
echo "数据库类型为:";
echo $config->database->type;
echo "<p>";
echo "数据库用户名为:";
echo $config->database->username;
echo "<p>";
echo "数据库密码为:";
echo $config->database->password;
echo "<p>";
Copy after login

Result:

The server address is: 127.0.0.1
The database type is: pdo_mysql
The database user name is: db_user
The database password is: db_pass

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the usage of Zend Framework action assistant Url

About the usage of Zend_Db_Table_Rowset in Zend Framework

The above is the detailed content of About the usage of Zend_Config component in Zend Framework. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix Microsoft NET Framework Installation Issues Error Code 0x800c0006 Fix May 05, 2023 pm 04:01 PM

.NET Framework 4 is required by developers and end users to run the latest versions of applications on Windows. However, while downloading and installing .NET Framework 4, many users complained that the installer stopped midway, displaying the following error message - " .NET Framework 4 has not been installed because Download failed with error code 0x800c0006 ". If you are also experiencing it while installing .NETFramework4 on your device then you are at the right place

How to identify Windows upgrade issues using SetupDiag on Windows 11/10 How to identify Windows upgrade issues using SetupDiag on Windows 11/10 Apr 17, 2023 am 10:07 AM

Whenever your Windows 11 or Windows 10 PC has an upgrade or update issue, you will usually see an error code indicating the actual reason behind the failure. However, sometimes confusion can arise when an upgrade or update fails without an error code being displayed. With handy error codes, you know exactly where the problem is so you can try to fix it. But since no error code appears, it becomes challenging to identify the issue and resolve it. This will take up a lot of your time to simply find out the reason behind the error. In this case, you can try using a dedicated tool called SetupDiag provided by Microsoft that helps you easily identify the real reason behind the error.

SCNotification has stopped working [5 steps to fix it] SCNotification has stopped working [5 steps to fix it] May 17, 2023 pm 09:35 PM

As a Windows user, you are likely to encounter SCNotification has stopped working error every time you start your computer. SCNotification.exe is a Microsoft system notification file that crashes every time you start your PC due to permission errors and network failures. This error is also known by its problematic event name. So you might not see this as SCNotification having stopped working, but as bug clr20r3. In this article, we will explore all the steps you need to take to fix SCNotification has stopped working so that it doesn’t bother you again. What is SCNotification.e

Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022 Microsoft .NET Framework 4.5.2, 4.6, and 4.6.1 will end support in April 2022 Apr 17, 2023 pm 02:25 PM

Microsoft Windows users who have installed Microsoft.NET version 4.5.2, 4.6, or 4.6.1 must install a newer version of the Microsoft Framework if they want Microsoft to support the framework through future product updates. According to Microsoft, all three frameworks will cease support on April 26, 2022. After the support date ends, the product will not receive "security fixes or technical support." Most home devices are kept up to date through Windows updates. These devices already have newer versions of frameworks installed, such as .NET Framework 4.8. Devices that are not updating automatically may

PHP Implementation Framework: Zend Framework Getting Started Tutorial PHP Implementation Framework: Zend Framework Getting Started Tutorial Jun 19, 2023 am 08:09 AM

PHP implementation framework: ZendFramework introductory tutorial ZendFramework is an open source website framework developed by PHP and is currently maintained by ZendTechnologies. ZendFramework adopts the MVC design pattern and provides a series of reusable code libraries to serve the implementation of Web2.0 applications and Web Serve. ZendFramework is very popular and respected by PHP developers and has a wide range of

KB5012643 for Windows 11 breaks .NET Framework 3.5 apps KB5012643 for Windows 11 breaks .NET Framework 3.5 apps May 09, 2023 pm 01:07 PM

It's been a week since we talked about the new safe mode bug affecting users who installed KB5012643 for Windows 11. This pesky issue didn't appear on the list of known issues Microsoft posted on launch day, thus catching everyone by surprise. Well, just when you thought things couldn't get any worse, Microsoft drops another bomb for users who have installed this cumulative update. Windows 11 Build 22000.652 causes more problems So the tech company is warning Windows 11 users that they may experience problems launching and using some .NET Framework 3.5 applications. Sound familiar? But please don't be surprised

How to use ACL (Access Control List) for permission control in Zend Framework How to use ACL (Access Control List) for permission control in Zend Framework Jul 29, 2023 am 09:24 AM

How to use ACL (AccessControlList) for permission control in Zend Framework Introduction: In a web application, permission control is a crucial function. It ensures that users can only access the pages and features they are authorized to access and prevents unauthorized access. The Zend framework provides a convenient way to implement permission control, using the ACL (AccessControlList) component. This article will introduce how to use ACL in Zend Framework

Cooler Master and Framework launch innovative mini case kit, compatible with laptop motherboards Cooler Master and Framework launch innovative mini case kit, compatible with laptop motherboards Dec 15, 2023 pm 05:35 PM

According to news on December 9, Cooler Master recently demonstrated a mini chassis kit in cooperation with notebook modular solution provider Framework at a demonstration event at the Taipei Compute Show. The unique thing about this kit is that it can be compatible with and Install the motherboard from the framework notebook. Currently, this product has begun to be sold on the market, priced at 39 US dollars, which is equivalent to approximately 279 yuan at the current exchange rate. The model number of this chassis kit is named "frameWORKMAINBOARDCASE". In terms of design, it embodies the ultimate compactness and practicality, measuring only 297x133x15 mm. Its original design is to be able to seamlessly connect to framework notebooks

See all articles