Simple factory mode text in ZendFramework

Source: Internet
Author: User
One day, I thought I hadn't reviewed the design pattern for a long time. After reviewing a design pattern, find the source code that uses this pattern in ZF. if you do not read all the source code, read the "advanced" part. let's talk about the pattern, I don't know if all models are available in ZF for the moment, but it should be used in ZF some time ago. I feel that it is not very good to treat him as a black box. I have always been eager to look at its source code,... If you look at it at, the challenge is indeed a little big. One day, I thought I hadn't reviewed the design pattern for a long time. After reviewing a design pattern, find the source code that uses this pattern in ZF. if you do not read all the source code, read the "advanced" part. let's talk about the pattern, I don't know if there are all models in ZF for the moment, but there should be enough models for me to read recently. I can look for other open source software to find the models. During this time, my life was a little messy by various written examinations, but no matter what, it is necessary to review. Let's talk about ZF. one advantage of ZF is that each component is relatively independent and there is not much dependency between component, which provides convenience for users, of course, it is also convenient for me to be bored and lazy to look at the source code.

Today, let's look at the simple factory. there is no shortage of models in ZF, and there is no shortage of factory models. the famous Zend_Db will use the simple factory without embarrassment, and then ctrl + h (under zend studio) we will find that there are a lot of factories, and most of them should be simple factories if you do not guess correctly. Because Zend_Db is the most commonly used, I naturally want to take a look at its implementation. Before viewing the source code, review how to use Zend_Db and the simple factory (here is a stack, first review the simple factory ).

Review simple factory models
Recall with a class chart:

Using a diagram from the author of the grinding design pattern, we can see that the Client obtains the object through the factory and calls it through the Api structure. Use factory to hide the creation of specific APIs. All other users only need to know how to create with factory and use the Api structure for calling. this is a simple review. The class chart should be able to remember the simple factory, because it is indeed very simple. After reviewing the simple factory, I thought a little bit and took a look at the use of Zend_Db.
1. review the use of Zend_Db
If you do not know how to use it, you have to check the source code of XXX but do not know how to use XXX? Why ?? Yun Yu> MU Xiaoyu? Use of end_Db. the following section is in the official ZF documentation (I personally do not like the ZF documentation very much, but I can't read Yii easily)
/Public/index. php
Copy codeThe code is as follows:
$ Db = Zend_Db: factory ('pdo _ mysql', array (
'Host' => '2017. 0.0.1 ',
'Username' => 'webuser ',
'Password' => 'xxxxxxxx ',
'Dbname' => 'test'
));

Here we put the database configuration in the code, which looks the simplest (in fact, it is not difficult for others, but the database is placed in different locations for ease of management ), however, this is not the best method under normal circumstances, but the simplest method is used here to highlight the key. Note the Zend_Db: factory ('pdo _ mysql '... This section
A $ db (A Zend_Db object) is generated above, and the above $ db is used for query as follows:
Copy codeThe code is as follows:
$ Db-> setFetchMode (Zend_Db: FETCH_OBJ );
$ Result = $ db-> fetchAssoc (
'Select bug_id, bug_description, bug_status FROM bugs'
);

Continue to the documents from the official website. this is the record retrieval mode, which is Object and fetch. now everything looks natural, but it still uses Zend_Db as a black box. Next, you can go to the topic.
First, check the zend/Db. php code summary:
Copy codeThe code is as follows:
<? Php
Class Zend_Db
{
/**
Set some constants and default values
*/
/**
* Factory for Zend_Db_Adapter_Abstract classes.
*
* First argument may be a string containing the base of the adapter class
* Name, e.g. 'mysqli' corresponds to class Zend_Db_Adapter_Mysqli. This
* Name is currently case-insensitive, but is not ideal to rely on this behavior.
* If your class is named 'My _ Company_Pdo_Mysql ', where 'My _ Company' is the namespace
* And 'pdo _ mysql' is the adapter name, it is best to use the name exactly as it
* Is defined in the class. This will ensure proper use of the factory API.
*
* First argument may alternatively be an object of type Zend_Config.
* The adapter class base name is read from the 'adapter 'property.
* The adapter config parameters are read from the 'params' property.
*
* Second argument is optional and may be an associative array of key-value
* Pairs. This is used as the argument to the adapter constructor.
*
* If the first argument is of type Zend_Config, it is assumed to contain
* All parameters, and the second argument is ignored.
*
* @ Param mixed $ adapter String name of base adapter class, or Zend_Config object.
* @ Param mixed $ config OPTIONAL; an array or Zend_Config object with adapter parameters.
* @ Return Zend_Db_Adapter_Abstract
* @ Throws Zend_Db_Exception
*/
Public static function factory ($ adapter, $ config = array ())
{
// Use the Zend_Config object. if the preceding method is not used, use Array directly.
If ($ config instanceof Zend_Config ){
$ Config = $ config-> toArray ();
}
/*
* Convert Zend_Config argument to plain string
* Adapter name and separate config object.
*/
If ($ adapter instanceof Zend_Config ){
If (isset ($ adapter-> params )){
$ Config = $ adapter-> params-> toArray ();
}
If (isset ($ adapter-> adapter )){
$ Adapter = (string) $ adapter-> adapter;
} Else {
$ Adapter = null;
}
}
/*
* Verify that adapter parameters are in an array.
*/
If (! Is_array ($ config )){
/**
* @ See Zend_Db_Exception
*/
Require_once 'zend/Db/Exception. php ';
Throw new Zend_Db_Exception (
'Adapter parameters must be in an array or a Zend_Config object ');
}
/*
* Verify that an adapter name has been specified.
*/
If (! Is_string ($ adapter) | empty ($ adapter )){
/**
* @ See Zend_Db_Exception
*/
Require_once 'zend/Db/Exception. php ';
Throw new Zend_Db_Exception (
'Adapter name must be specified in a String ');
}
/*
* Form full adapter class name
*/
$ AdapterNamespace = 'zend _ Db_Adapter ';
If (isset ($ config ['adapternamespace']) {
If ($ config ['adapternamespace ']! = ''){
$ AdapterNamespace = $ config ['adapternamespace'];
}
Unset ($ config ['adapternamespace ']);
}
// Adapter no longer normalized-see http://framework.zend.com/issues/browse/ZF-5606
$ AdapterName = $ adapterNamespace .'_';
$ AdapterName. = str_replace ('','_',
Ucwords (str_replace ('_', '', strtolower ($ adapter ))));
/*
* Load the adapter class. This throws an exception
* If the specified class cannot be loaded.
*/
If (! Class_exists ($ adapterName )){
Require_once 'zend/Loader. php ';
Zend_Loader: loadClass ($ adapterName );
}
/*
* Create an instance of the adapter class.
* Pass the config to the adapter class constructor.
*/
$ DbAdapter = new $ adapterName ($ config );
/*
* Verify that the object created is a descendent of the abstract adapter type.
*/
If (! $ DbAdapter instanceof Zend_Db_Adapter_Abstract ){
/**
* @ See Zend_Db_Exception
*/
Require_once 'zend/Db/Exception. php ';
Throw new Zend_Db_Exception (
"Adapter class '$ adapterName' does not extend Zend_Db_Adapter_Abstract ");
}
Return $ dbAdapter;
}
}

The comment at the top is worth looking at. it clearly illustrates the factory. The other important sections of code (ignore exception handling) are:
Copy codeThe code is as follows:
// The factory has a parameter named $ adapter.
Public static function factory ($ adapter, $ config = array ())

// Determine namespace
$ AdapterNamespace = 'zend _ Db_Adapter ';

// Construct the class name using namespace and the $ adapter passed in above
$ AdapterName = $ adapterNamespace .'_';
$ AdapterName. = str_replace ('', '_', ucwords (str_replace ('_','', strtolower ($ adapter ))));

// Use the Class name generated above to generate obj. it seems PHP is a little more convenient than java (Class. forName ('XXX'). newInstance ())
$ DbAdapter = new $ adapterName ($ config );

In retrospect, where Zend_Db: factory is used to generate $ db:
Copy codeThe code is as follows:
$ Db = Zend_Db: factory ('pdo _ mysql', array (
'Host' => '2017. 0.0.1 ',
'Username' => 'webuser ',
'Password' => 'xxxxxxxx ',
'Dbname' => 'test'
));

The first parameter of the factory method is $ adapter, which is Pdo_Mysql. remember Pdo_Mysql. skip the step and follow the above $ adapterNamespace = 'zend _ Db_Adapter '; we can see that the generated $ dbAdapter value must eventually be Zend_Db_Adapter_Pdo_Mysql, OK. find the zend/db/adapter/pdo directory under this name. ha, there are so many familiar faces, I have seen familiar old faces such as MySql, Mssql, and Sqlite.


Note: There is a low-profile Abstract. php in it, and their parent class Zend_Db_Adapter_Pdo_Abstract. Open Mysql. php and you can see
Class Zend_Db_Adapter_Pdo_Mysql extends Zend_Db_Adapter_Pdo_Abstract

Well, the class name Zend_Db_Adapter_Pdo_Mysql is the same as the name generated above. when you look at the classes in several other files, they all inherit from Zend_Db_Adapter_Pdo_Abstract. if you want to draw a class chart, the following class diagram should be created:

Then add Zend_Db, where the Client and factory functions are called. this simple class diagram should be,

A very pure simple factory is like this (unlike a simple factory chart? That's because the class is not properly positioned ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.