


Zend Framework tutorial Zend_Registry object usage analysis, zendzend_registry_PHP tutorial
Jul 12, 2016 am 08:56 AMZend Framework tutorial Zend_Registry object usage analysis, zendzend_registry
This article describes the Zend Framework tutorial Zend_Registry object usage with examples. Share it with everyone for your reference, the details are as follows:
Use object registry (Registry)
The object registry (or object warehouse) is a container used to store objects and values ??throughout the application space. By storing objects in it, we can use the same object anywhere throughout the project. This mechanism is equivalent to a global storage.
We can use the object registry through the static methods of the Zend_Registry class. In addition, since this class is an array object, you can use the array form to access the class methods.
1. Set the value in Registry
To save an item to the registry, we can use the static method set().
Example 1. Set() usage example:
Zend_Registry::set('index', $value);
$value can be an object, array or scalar. You can use set() again to set a new value to a value already in the registry.
The index parameter can be a scalar, a string or an integer, just like using an array, similar to the index/key name of the array.
2. Get the value in Registry
You can use the get() method to get the value of an item in the Registry.
Example 2. get() method example:
$value = Zend_Registry::get('index');
getInstance() returns a static registry object.
Registry objects are iterable.
Example 3. Iterate a registry object:
$registry = Zend_Registry::getInstance(); foreach ($registry as $index => $value) { echo "Registry index $index contains:/n"; var_dump($value); }
3. Create a Registry object
In addition to using static methods to access the Registry object, you can instantiate it directly, just like using a normal object.
If you access the instance of the registry object through a static method, it is convenient for static storage, and you can access it anywhere in the program.
If you use the traditional new method to create an instance of the registry, you can use an array to initialize the contents of the registry.
Example 4. Create a registry object
$registry = new Zend_Registry(array('index' => $value));
After creating this object instance, you can use the array object method to use it, or you can set this object instance to a static object instance through the static method setInstance().
Example 5. Example of initializing the static registry
$registry = new Zend_Registry(array('index' => $value)); Zend_Registry::setInstance($registry);
If the static registry object has been initialized, the setInstance() method will throw a Zend_Exception.
4. Access the Registry object like an array
If you want to access or set multiple values ??at once, you will find it convenient to use the array method.
Example 6. Array access example:
$registry = Zend_Registry::getInstance(); $registry['index'] = $value; var_dump( $registry['index'] );
5. Access the Registry in object mode
You will find it convenient to access registry objects using an object-oriented style, using the property names in the object as indexes. To do this, you need to create the registry object using the ArrayObject::ARRAY_AS_PROPS option and initialize the static instance. You need to do this before the static registry is accessed for the first time. Be careful with this option, as some versions of PHP have bugs when using this option.
Example 7. Access in object form:
//在你的bootstrap代碼中: $registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS) Zend_Registry::setInstance($registry); $registry->tree = 'apple'; . . . //在程序的任何其它地方: $registry = Zend_Registry::getInstance(); echo $registry->tree; // echo's "apple" $registry->index = $value; var_dump($registry->index);
6. Query whether an index exists
You can use the static method isRegistered() to query whether a specific index has a corresponding value set.
Example 8. isRegistered() Example:
if (Zend_Registry::isRegistered($index)) { $value = Zend_Registry::get($index); }
To determine whether the value of a specific index in an array object is set, you can use the isset() function, just like you would use it in a normal array.
Example 9. isset() Example:
$registry = Zend_Registry::getInstance(); // using array-access syntax if (isset($registry['index'])) { var_dump( $registry['index'] ); } // using object-access syntax, if enabled if (isset($registry->index)) { var_dump( $registry->index ); }
7. Extend Registry object
Static registry object is an instance of class Zend_Registry. If you want to add functionality to it, you can inherit the Zend_Registry class and then specify to use this class to access the object registry. You can use the static method setClassName() to specify the class to use. Note that this class must be a subclass of Zend_Registry.
Example 10. Specify the class name of the static registry:
Zend_Registry::setClassName('My_Registry'); Zend_Registry::set('index', $value);
If you try to set the class name after the registry has been accessed, the registry throws an exception. It is recommended that you set this class name in the boostrap code (i.e. index.php).
8. Delete static registry
Although this is not required, you can use the _unsetInstance() method to delete a static instance of the registry.
[Note] Risk of data loss
When using _unsetInstance(), all data in the static registry will be lost and cannot be recovered.
Sometimes you may need the _unsetInstance() method. For example, if you want to use setInstance() or setClassName() after the registry object has been initialized, you can use _unsetInstance() to delete the static instance before using those methods.
Example 11. _unsetInstance() Example:
Zend_Registry::set('index', $value); Zend_Registry::_unsetInstance(); // 改變我們要使用的類 Zend_Registry::setClassName('My_Registry'); Zend_Registry::set('index', $value);
更多關(guān)于zend相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Zend FrameWork框架入門教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《Yii框架入門及常用技巧總結(jié)》、《ThinkPHP入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Zend Framework框架的PHP程序設(shè)計有所幫助。
您可能感興趣的文章:
- Zend 輸出產(chǎn)生XML解析錯誤
- 基于Zend的Config機制的應(yīng)用分析
- Zend Framework實現(xiàn)多服務(wù)器共享SESSION數(shù)據(jù)的方法
- Zend Framework框架Smarty擴展實現(xiàn)方法
- Zend Framework框架路由機制代碼分析
- Zend Framework實現(xiàn)留言本分頁功能(附demo源碼下載)
- Zend Framework實現(xiàn)將session存儲在memcache中的方法
- Zend Framework分頁類用法詳解
- Zend Framework生成驗證碼并實現(xiàn)驗證碼驗證功能(附demo源碼下載)
- Zend Framework實現(xiàn)多文件上傳功能實例
- Zend Framework入門之環(huán)境配置及第一個Hello World示例(附demo源碼下載)
- Zend Framework入門知識點小結(jié)
- Zend Framework教程之Zend_Config_Xml用法分析

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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.

.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
![SCNotification has stopped working [5 steps to fix it]](https://img.php.cn/upload/article/000/887/227/168433050522031.png?x-oss-process=image/resize,m_fill,h_207,w_330)
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 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: 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

1. Press and hold the [Win+R] shortcut key combination on the keyboard, open the [Run] dialogue command window, enter the [services.msc] command, and click [OK];. 2. After opening the service interface, find the [RemoteRegistry] option, and double-click with the left button to open its properties dialog window. 3. In the [RemoteRegistry Properties] dialog window that opens, select the [Disabled] option in the startup type option, and then click the [Apply]--[Stop]--[OK] button to save the settings.

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

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
