Available Languages
Manual for Zend Framework 1.0.3
Zend_Config は、アプリケーションの設定データを
簡単に使用できるようにするために設計されたものです。
階層構造になったプロパティを使用して、設定データを簡単に
アプリケーションに読み込めるようになっています。
設定データは、階層構造のデータ保存をサポートしている
さまざまな媒体から読み込むことができます。
現時点で Zend_Config が提供している設定データアダプタは
Zend_Config_Ini
と Zend_Config_Xml
の二種類で、テキストファイルに格納された設定データを使用できるようになっています。.
例 5.1. Zend_Config の使用例
通常は、Zend_Config_Ini
あるいは Zend_Config_Xml
のようなアダプタクラスを使用することが想定されています。
しかし、もし設定データが PHP の配列として存在するのなら、
単にそれを Zend_Config のコンストラクタに渡すだけで、
シンプルなオブジェクト指向のインターフェイスを使用することができます。
<?php
// 設定データは配列で渡されます
$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);
// 設定データを表示します (結果は 'www.example.com' となります)
echo $config->webhost;
// 設定データを使用してデータベースに接続します
$db = Zend_Db::factory($config->database->adapter,
$config->database->params->toArray());
// もうひとつの方法: 単に Zend_Config オブジェクトを渡します
// Zend_Db のファクトリは、その処理方法を知っています
$db = Zend_Db::factory($config->database);
上の例で説明したように、Zend_Config を使用すると、
コンストラクタに渡されたデータについて、
階層化されたプロパティの形式でアクセスできるようになります。
このようにオブジェクト思考形式でデータの値にアクセスするだけでなく、
Zend_Config では get() メソッドも用意しています。
これは、指定した要素が存在しない場合にデフォルト値を返すように設定できます。
たとえば次のように使用します。
<?php
$host = $config->database->get('host', 'localhost');
例 5.2. Zend_Config における PHP 設定ファイルの使用法
設定ファイルそのものを PHP で書きたいこともあるでしょう。 以下のようにすると、簡単にそれを行うことができます。
<?php
// config.php
return array(
'webhost' => 'www.example.com',
'database' => array(
'adapter' => 'pdo_mysql',
'params' => array(
'host' => 'db.example.com',
'username' => 'dbuser',
'password' => 'secret',
'dbname' => 'mydatabase'
)
)
);
<?php // 設定を読み込みます require_once 'Zend/Config.php'; $config = new Zend_Config(require 'config.php'); // 設定データを出力します (この結果は 'www.example.com' です) echo $config->webhost;
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
