Zendframework的註冊模式

2021-06-01 02:47:33 字數 2221 閱讀 7411

有段時間沒更新部落格了,最近一段時間在搗鼓框架,幾個框架倒來倒去的。主要的還是研究了zend framework。現介紹下zend framework的註冊模式。

很多時候,有經驗的php程式設計師都告誡我們,慎用全域性變數,全域性變數不好管理呀。稍不注意就會致使全域性變數濫用。zend framework 提供了乙個註冊模式來對全域性變數進行管理。它的實質是類似對乙個陣列進行操作。首先註冊變數,把全域性變數set進陣列,取的時候get 全域性變數。其中它繼承了spl 的arrayobject類來進行操作。註冊模式一般會跟單例模式一起用。為了保證註冊唯一呀。下面貼上zend framework的register**。

<?php

classzend_registry extends arrayobject

private static $_registryclassname ='zend_registry';

private static $_registry = null;

//得到單例

public static function getinstance()

if (self::$_registry === null) arrayobject

//初始化

protected static function init()

self::setinstance(newself::$_registryclassname());

public static functionsetclassname($registryclassname = 'zend_registry')

if (self::$_registry !== null) {

require_once 'zend/exception.php';

throw new zend_exception('registryis already initialized');

if (!is_string($registryclassname)) {

require_once 'zend/exception.php';

throw newzend_exception("argument is not a class name");

* @see zend_loader

require_once 'zend/loader.php';

zend_loader::loadclass($registryclassname);

self::$_registryclassname =$registryclassname;

//登出單例

public static function _unsetinstance()

self::$_registry = null;

//得到已註冊的資料

public static function get($index)

$instance = self::getinstance();

if(!$instance->offsetexists($index)) {

require_once 'zend/exception.php';

throw new zend_exception("noentry is registered for key '$index'");

return $instance->offsetget($index);

//設定值,索引與值對應

public static function set($index, $value)

$instance = self::getinstance();

$instance->offsetset($index, $value);

//判斷是否已註冊

public static function isregistered($index)

if (self::$_registry === null) {

return false;

returnself::$_registry->offsetexists($index);

public function __construct($array =array(), $flags = parent::array_as_props)

parent::__construct($array, $flags);

//判斷是否已存在

public function offsetexists($index)

return array_key_exists($index, $this);

zend framework學習小結

zend framework是mvc模式的一種實現,要快速的入門差不多只看zend controller zend view 部分就可以了吧。1.zend controller部分。最重要的類是zend controller front.使用它的經典 這部分是包含在index。php中的。在正確的配...

zend framework學習小結

zend framework是mvc模式的一種實現,要快速的入門差不多只看zend controller zend view 部分就可以了吧。1.zend controller部分。最重要的類是zend controller front.使用它的經典 很 簡單 這部分是包含在index。php中的。...

zend framework常用元件

zend acl 許可權控制 zend auth 主要用於認證 zend cache 為應用程式提供快取支援 zend config 應用程式的配置資料引數 zend db 提供zend framework 與mysql的資料庫操作方式 zend layout 實現應用程式的試圖布局 zend ma...