Simple Factory mode Graphics _php Tutorial in the Zend framework

Source: Internet
Author: User
Tags zend framework
Some time ago to ZF, the black box when he felt not very good, has been to see its source of impulse, but ... If you look at it at 1.1, the challenge is really a little bit bigger. One day and then thought for a long time did not review design patterns. Comprehensive, review a design mode after the ZF in the use of the source of this mode, do not read all the source code, read the comparison of "advanced" part of it, to say that the mode, for the time being do not know that all models ZF inside have, but there should be enough mode for me to see recently, in said can find other open source software to find This period of time by a variety of written Test God horse a mess of life, but in any case, review is necessary. Again ZF, ZF one advantage is that each component more independent, component not too much reliance, so that the user provides a convenient, of course, I am so bored and lazy to see the source of people to provide convenience.

Today, look at the simple factory, ZF inside no lack of mode, not lack of factory model, the famous zend_db is not stingy with the use of simple factory, and then Ctrl+h (Zend Studio) will find factory particularly many, if not wrong should most should also be a simple factory. Since zend_db is most commonly used, I will naturally be more interested in seeing his realization. Review the source code before reviewing how to use zend_db and simple factory (here is a stack, review the Simple factory first).

Review Simple Factory mode
Recall with class diagram, Simple Factory class diagram:

Using a graph from the author of the grinding design pattern, you can see that the client obtains the object through factory and invokes it through the API structure. Use factory to hide the creation of specific APIs. While all other users in use, only need to know to create with factory, through the API structure call, simple review completed. Seeing a class diagram should be reminiscent of a simple factory, because he is really simple. After reviewing the simple factory, think a little jump, take a direct look at the use of zend_db.
1. Review the use of zend_db
If you do not know how to use, ready to see the source of xxx, but do not know how to use XXX, this is a bit embarrassing, so the first small look at the use of zend_db, the following paragraph is in the official ZF documents (individuals are not very like ZF documents, not yii easy to read)
/public/index.php
Copy CodeThe code is as follows:
$db = zend_db::factory (' Pdo_mysql ', Array (
' Host ' = ' 127.0.0.1 ',
' Username ' = ' webuser ',
' Password ' = ' xxxxxxxx ',
' dbname ' = ' test '
));

Here is the database configuration also put in the code, it looks the simplest (in fact, the other is not difficult, but the location of the database placement is different, easy to manage), but this is not the best way in normal circumstances, but in order to highlight the focus, here the simplest way to choose. Notice the inside of the Zend_db::factory (' Pdo_mysql ' ... This paragraph
The above generates a $db (a zend_db object), using the $db above to query the following:
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 from the official website document, this is to take the record mode as object, then fetch, everything seems to be natural now, but still use it zend_db as a black box. Below you can get to the point.
First, take a look at the code summary for zend/db.php:
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, and is not ideal-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's best for use the name exactly as it
* is defined in the class. This would ensure proper use of the factory API.
*
* First argument may alternatively is an object of type Zend_config.
* The adapter class base name is read from the ' Adapter ' property.
* The adapter config parameters is read from the "params" property.
*
* Second argument is optional and could be an associative array of key-value
* Pairs. This was used as the argument to the adapter constructor.
*
* If The first argument is of type zend_config, it's assumed to contain
* All parameters, and the second argument are 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 ())
{
Using the Zend_config object, the above method is not used, directly using the array
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 is 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 is in an array or a Zend_config object ');
}
/*
* Verify that a 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 is 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 top note is well worth seeing, it clearly illustrates the factory, and another important piece of code (ignoring exception handling) is:
Copy CodeThe code is as follows:
Factory has a parameter called $adapter.
public static function Factory ($adapter, $config = Array ())

Determine namespace
$adapterNamespace = ' Zend_db_adapter ';

Construct the class name with namespace and the $adapter passed in above
$adapterName = $adapterNamespace. '_';
$adapterName. = Str_replace (', ' _ ', Ucwords (Str_replace (' _ ', ' ', Strtolower ($adapter))));

Using the generated class name new out of obj, PHP appears to be a little bit more convenient than Java (Class.forName (' XXX '). newinstance ())
$dbAdapter = new $adapterName ($config);

Recall the place where you used zend_db::factory to generate $DB:
Copy CodeThe code is as follows:
$db = zend_db::factory (' Pdo_mysql ', Array (
' Host ' = ' 127.0.0.1 ',
' Username ' = ' webuser ',
' Password ' = ' xxxxxxxx ',
' dbname ' = ' test '
));

The first parameter of the factory method is $adapter for Pdo_mysql, remember here is Pdo_mysql, then jump again, according to the above $adapternamespace = ' zend_db_adapter '; you can see the generated found $ Dbadapter the value of the final must be: Zend_db_adapter_pdo_mysql,ok, according to this name to find Zend/db/adapter/pdo directory, ha, so many familiar faces, see the familiar Mysql, Mssql, SQLite these old faces.


Note that there is also a low-key abstract.php, inside their parent class zend_db_adapter_pdo_abstract. Open mysql.php to 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, in the other several files inside the class, they all inherit from Zend_db_adapter_pdo_abstract, if you want to draw class diagram, Then there should be a class diagram like this:

Then add the call to the client and the factory function where the location of zend_db, this simple class diagram should be,

A very, very pure simple factory just came out (unlike a simple factory class diagram?). That's just because the position of the class is not set.

http://www.bkjia.com/PHPjc/325670.html www.bkjia.com true http://www.bkjia.com/PHPjc/325670.html techarticle some time ago to ZF, the black box when he felt not very good, has been to see its source of impulse, but ... If you look at it at 1.1, the challenge is really a little bit bigger. One day and then think ...

  • 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.