Coding Samples, Tutorials, Code Snippets, Articles, How-To`s - From Beginners to Expert. Stay tuned.
Share your knowledge. Regardless what experience you have made with the Zend Framework - Let all benefit.

Search Manual:

Please Sign in or Register

Available Languages

 


Manual for Zend Framework 1.0.3

5.4. Zend_Config_Xml

Zend_Config_Xml を使用すると、シンプルな XML 形式で保存した設定データを、オブジェクトのプロパティとして扱えるようになります。 XML のルート要素は設定には関係しないので、任意の名前がつけられます。 その直下のレベルの XML 要素が設定データのセクションに対応します。 セクションレベルの要素の下に XML 要素を配置することで、 階層構造をサポートします。 末端レベルの XML 要素が設定データの値に対応します。 セクションの継承は、XML の属性 extends でサポートされます。この属性の値が、 データを継承しているセクション名を表します。

[注意] 返り値の型

Zend_Config_Xml で読み込んだ設定データは、 常に文字列形式で返されます。必要に応じて、 文字列から適切な型に変換してください。

例 5.4. Zend_Config_Xml の使用法

この例は、Zend_Config_Xml を使用して XML ファイルから設定データを読み込むための基本的な方法を説明するものです。 この例では、運用環境の設定と開発環境の設定を両方管理しています。 開発環境用の設定データは運用環境用のものと非常に似ているので、 開発環境用のセクションは運用環境用のセクションを継承させています。 今回の場合なら、逆に運用環境用のセクションを開発環境用のものから継承させてもいいでしょう。 そうしたからといって特に状況が複雑になるわけではありません。 ここでは、次のような内容の設定データが /path/to/config.xml に存在するものとします。

<?xml version="1.0"?>
<configdata>
    <production>
        <webhost>www.example.com</webhost>
        <database>
            <type>pdo_mysql</type>
            <host>db.example.com</host>
            <username>dbuser</username>
            <password>secret</password>
            <name>dbname</name>
        </database>
    </production>
    <staging extends="production">
        <database>
            <host>dev.example.com</host>
            <username>devuser</username>
            <password>devsecret</password>
        </database>
    </staging>
</configdata>
        

次に、開発者が開発環境用の設定データを XML ファイルから読み込むことを考えます。 これは簡単なことで、単に XML ファイルとセクションを指定するだけです。

<?php
require_once 'Zend/Config/Xml.php';

$config = new Zend_Config_Xml('/path/to/config.xml', 'staging');

echo $config->database->host; // "dev.example.com" と出力します
echo $config->database->name; // "dbname" と出力します
        

Welcome!

Welcome to ZFResource - The Resource for the Zend Framework.
As you see, the website is in still in progress. Many features ( like Code Directory, Code Samples, User written Tutorials, Examples and News,...) will be online in near future.
At the moment, you can search and browse the manual in your language of choice. Don't forget to come back later to benefit from the new features.


Help Wanted:

We are still searching for people want to help building this site. If you want to see this website in your language or you have any suggestions for this site please send us an email