Home Backend Development PHP Tutorial Zend的Registry机制的使用说明_PHP

Zend的Registry机制的使用说明_PHP

Jun 01, 2016 pm 12:06 PM
registry zend

项目过程中有很多全局变量, 需要全局存储,是否是使用全局变量来进行存储?那就弱爆了。Zend使用Registry机制(注册表)存储对象和值,是一个存储对象和值的容器。

Zend_Registry这个类就是做这个目的

代码示例
Zend_Registry::set('config', $config);
Zend_Registry::get('config');

代码分析
这两个函数是最常用的两个函数。我们来看一下这个类

class Zend_Registry extends ArrayObject

这个类继承自ArrayObject

ArrayObject implements IteratorAggregate , Traversable , ArrayAccess , Serializable , Countable

ArrayObject是一个对象集合,相当于其他语言的泛型集合的概念。

重点了解下void ArrayObject::offsetSet ( mixed $index , mixed $newval ), 这个函数就是hashtable中的设置key,value,只是key,value可以是任何类型的。

好了,回到Zend_Registry, 看看set做了些什么事情

set函数
复制代码 代码如下:
public static function set($index, $value)
    {
        $instance = self::getInstance();
        $instance->offsetSet($index, $value);

    }


一个是实例化Register,另一个是调用offsetSet方法,将index和value设置进去。

offset方法很好理解,但是为什么要使用getInstance方法呢?

这里建议大家好好看看,这个是结合类静态方法的单例模式。

我们一般的单例模式写成:
复制代码 代码如下:
class A{
    private $_instance;
    public static function getInstance(){
        ...
    }

    protected function __construct(){
        ...
    }

    public function setVal(){
        ...
    }
}

$a = A::getInstance();

$a->setVal();

这样在调用之前就需要实例化一个类,虽然这个实例化实际上是单例,但感觉还是不舒服

这边的register就做到了直接使用静态方法调用

A::setVal();

大致的代码思路我写了个demo
复制代码 代码如下:
class A{
    private static $_instance;
    public static function getInstance(){
        if(self::_instance !==null){
            return $this->_instance;
        } else {
            return new A();
        }
    }

    public function __construct(){

    }

    public static function setV(){
        $a = self::getInstance();
        $a->setVal();
    }

    public function setVal(){
        ...
    }
}

A::setV();

实际上就是直接把__construct()放开成为public,然后实例化它

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The operation process of closing the registry process in WIN10 The operation process of closing the registry process in WIN10 Mar 27, 2024 pm 05:51 PM

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.

PHP Implementation Framework: Zend Framework Getting Started Tutorial PHP Implementation Framework: Zend Framework Getting Started Tutorial Jun 19, 2023 am 08:09 AM

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

How to use ACL (Access Control List) for permission control in Zend Framework How to use ACL (Access Control List) for permission control in Zend Framework Jul 29, 2023 am 09:24 AM

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

PHP does not recognize ZendOptimizer, how to solve it? PHP does not recognize ZendOptimizer, how to solve it? Mar 19, 2024 pm 01:09 PM

PHP does not recognize ZendOptimizer, how to solve it? In PHP development, sometimes you may encounter a situation where PHP cannot recognize ZendOptimizer, which will cause some PHP codes to not run properly. In this case, we need to take some measures to solve the problem. Some possible workarounds are described below, along with specific code examples. 1. Confirm whether ZendOptimizer is installed correctly: First, we need to confirm that ZendOptimizer

How to configure the Window2003 IIS+MySQL+PHP+Zend environment How to configure the Window2003 IIS+MySQL+PHP+Zend environment Jun 02, 2023 pm 09:56 PM

The Windows 2003 installation package includes Zend, PHP5.2.17, PHPWind8.7 and PHPMyadmin3.5.2. You can download the installation package directly to save time searching for resources. However, since MySQL has exceeded the upload limit, you need to go to the MySQL official website to download. Then unzip and copy to the D drive, as shown below: MySQLinDdisk Install and configure WindowsIIS+FTP Click Start>Control Panel>Add or Remove Programs.AddingordeletingaPG Click Add/Remove Windows Components (A). Addingorde

What program is registry? What program is registry? Feb 24, 2023 pm 05:21 PM

Registry refers to "registry editor" and is a system program. The registry is an important database in the Microsoft Windows system. It is used to store system and application setting information, including configuration and status information about the user's computer software and hardware, and the initial conditions of the application and resource manager shells. , preferences and uninstall data, the computer's overall system settings and various permissions, the association of file extensions with applications, the description, status and properties of the hardware, etc.

Develop a high-performance search engine using the PHP framework Zend Develop a high-performance search engine using the PHP framework Zend Jun 27, 2023 am 08:36 AM

With the explosive growth of Internet information, search engines have become one of the preferred ways for people to obtain information. Now, as the number of websites continues to increase, the rapid response and accuracy of search engines have become more and more important, which requires search engines to have high performance. In this article, I will introduce how to use the PHP framework Zend to develop a high-performance search engine. 1. Why use Zend Framework? Zend Framework is a high-performance PHP framework that has excellent performance and scalability.

How to use the PHP framework Zend to develop an efficient ERP management platform How to use the PHP framework Zend to develop an efficient ERP management platform Jun 26, 2023 pm 11:00 PM

With the rapid development of information technology, more and more enterprises are beginning to realize the necessity of information management. ERP (Enterprise Resource Planning) management platform is an important tool for modern enterprise management, which can help enterprises realize resource planning, collaboration, control, optimization and management. Among them, the PHP framework Zend is an excellent development tool that can help developers develop ERP systems quickly and efficiently. This article will introduce how to use Zend to develop an efficient ERP management platform. 1. Determine requirements analysis before starting the development process

See all articles