Simple factory mode text in Zend Framework

Source: Internet
Author: User

Some time ago, I used ZF to treat him as a black box. I feel that it is not very good. I have always been eager to look at the 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 are prepared to read the source code of XXX, but you do not know how to use XXX. This is a bit confusing, so let's take a look at the use of Zend_Db, the following section is in the official documents of ZF (I personally do not like the documents of ZF very much, but I do not have to read Yii easily)
/Public/index. phpCopy 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.