、代わりに特別な場合を除き、Zend_Registryを使用してのあなたにもできるように、パブリックメンバ関数で、すべてのアプリケーションの情報が含まれていますシングルトンApplicationクラスを作成することができます関連するデータにアクセスする必要があります。あなたは、関連するコードとスニペットを見ることができます(それはあなたにそれがどのように実装できるかのアイデアを与えることを、そのまま実行されません):
final class Application
{
/**
* @var Zend_Config
*/
private $config = null;
/**
* @var Application
*/
private static $application;
// snip
/**
* @return Zend_Config
*/
public function getConfig()
{
if (!$this->config instanceof Zend_Config) {
$this->initConfig();
}
return $this->config;
}
/**
* @return Application
*/
public static function getInstance()
{
if (self::$application === null) {
self::$application = new Application();
}
return self::$application;
}
/**
* Load Configuration
*/
private function initConfig()
{
$configFile = $this->appDir . '/config/application.xml';
if (!is_readable($configFile)) {
throw new Application_Exception('Config file "' . $configFile . '" is not readable');
}
$config = new Zend_Config_Xml($configFile, 'test');
$this->config = $config;
}
// snip
/**
* @param string $appDir
*/
public function init($appDir)
{
$this->appDir = $appDir;
$this->initConfig();
// snip
}
public function run ($appDir)
{
$this->init($appDir);
$front = $this->initController();
$front->dispatch();
}
}
あなたのブートストラップは、次のようになります。
require 'Application.php';
try {
Application::getInstance()->run(dirname(dirname(__FILE__)));
} catch (Exception $e) {
header("HTTP/1.x 500 Internal Server Error");
trigger_error('Application Error : '.$e->getMessage(), E_USER_ERROR);
}
あなたは以下を使用する設定にアクセスする場合:
$var = Application::getInstance()->getConfig()->somevar;
これは、アレイをオブジェクトに再解析するために少し時間がかかります。あなたが配列としての設定を好むなら、それは単なる "Zend_Registry :: set( 'config'、$ this-> getOptions());"値を取得する前に、変数に値を取得する必要があります。 –
@Alister:そうです。オプション配列をレジストリの中に格納するほうが速いのですが、単一の値を取得するたびに配列を格納するのは面倒です。 –
これは、以下の$ GLOBALS ['application']のアイデアと変わらないもので、$ GLOBALS ['application']がおそらく99%働いているという利点があります。 –