Home  >  Article  >  Backend Development  >  Instructions for using Zend's Registry mechanism_PHP tutorial

Instructions for using Zend's Registry mechanism_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:10:20762browse

There are many global variables in the project process, which need to be stored globally. Are global variables used for storage? That would be too weak. Zend uses the Registry mechanism (registry) to store objects and values, and is a container for storing objects and values.

The Zend_Registry class is used for this purpose

Code Example
Zend_Registry::set('config', $config);
Zend_Registry::get('config');

Code Analysis
These two functions are the two most commonly used functions. Let’s take a look at this class

class Zend_Registry extends ArrayObject

This class inherits from ArrayObject

ArrayObject implements IteratorAggregate , Traversable , ArrayAccess , Serializable , Countable

ArrayObject is a collection of objects, equivalent to the concept of generic collections in other languages.

Focus on the void ArrayObject::offsetSet (mixed $index, mixed $newval). This function is to set the key and value in the hashtable. It is just the key and the value can be of any type.

Okay, go back to Zend_Registry and see what set has done

set function

Copy code The code is as follows:

public static function set($index , $value)
{
$instance = self::getInstance();
$instance->offsetSet($index, $value);

}


One is to instantiate Register, and the other is to call the offsetSet method to set the index and value.

The offset method is easy to understand, but why use the getInstance method?

I suggest you take a closer look. This is a singleton pattern combined with class static methods.

Our general singleton pattern is written as:

Copy code The code is as follows:

class A{
private $_instance;
public static function getInstance(){
...
}

protected function __construct(){
...
}

public function setVal(){
...
}
}

$a = A::getInstance();

$ a->setVal();

In this way, a class needs to be instantiated before calling. Although this instantiation is actually a singleton, it still feels uncomfortable

The register here can be called directly using static methods

A::setVal();

I wrote a demo for the general code idea

Copy the code The code is as follows:

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();

In fact, it is to directly make __construct() public and then instantiate it

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327073.htmlTechArticleThere are many global variables during the project, which require global storage. Are global variables used for storage? That would be too weak. Zend uses the Registry mechanism (registry) to store objects and values,...
Statement:
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