search
HomeBackend DevelopmentPHP TutorialSimple Factory Pattern in Zend Framework Graphics_PHP Tutorial

Simple Factory Pattern in Zend Framework Graphics_PHP Tutorial

Jul 21, 2016 pm 03:17 PM
frameworkzendnoGraphics and textfactoryhavemodellookSimple

I used ZF some time ago, and it didn't feel very good to treat it as a black box. I always had the urge to look at its source code, but. . . If you look at it bit by bit, the challenge is indeed a bit big. One day it occurred to me that I haven't reviewed design patterns for a long time. To sum it up, after reviewing a design pattern, look for the source code that uses this pattern in ZF. Don’t read all the source code, but read the more “advanced” parts. When it comes to patterns, I don’t know if all patterns are included in ZF. , but there should be enough patterns. I have looked at it recently, and I am saying that I can look for other open source software to find patterns. During this period of time, all kinds of written examinations have made my life a little chaotic, but no matter what, review is still necessary. Let’s talk about ZF again. One of the advantages of ZF is that each component is relatively independent, and there is not too much dependence between components. This provides convenience for users, and of course it also provides convenience for boring and lazy people like me who want to read the source code. convenient.

Let’s take a look at the simple factory today. There is no shortage of patterns in ZF, let alone factory patterns. The famous Zend_Db uses simple factories without hesitation. Then ctrl+h (under zend studio) you will find factory There are a lot of them. If I guess correctly, most of them should be simple factories. Since Zend_Db is the most commonly used, I naturally want to see its implementation. Before looking at the source code, first review how to use Zend_Db and a simple factory (this is a stack, review the simple factory first).

Review the simple factory pattern
Use a class diagram to recall the simple factory class diagram:

Borrowing a picture from the author of "Grinding Design Pattern", you 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. When all other users use it, they only need to know how to create it with the factory and call it through the Api structure. A simple review is completed. When you see the class diagram, you should think of a simple factory, because it is indeed very simple. After reviewing the simple factory, let’s jump in a little and let’s look directly at the use of Zend_Db.
1. Review the use of Zend_Db
If you don’t know how to use it, it’s a bit embarrassing to look at the source code of XXX but don’t know how to use XXX, so let’s take a brief look at the use of Zend_Db. The following paragraph is in From ZF official documents (I personally don’t like ZF documents very much, they are not as easy to read as Yii)
/public/index.php
Copy code The 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 into the code Inside, it looks the simplest (actually the others are not difficult, it’s just that the database is placed in a different location for easier management), but this is not the best way under normal circumstances, but in order to highlight the key points, the simplest one is chosen here. Way. Pay attention to the Zend_Db::factory('Pdo_Mysql'... This paragraph
generates a $db (a Zend_Db object) above. Use the above $db to query as follows:
Copy the code The code is as follows:

$db->setFetchMode(Zend_Db::FETCH_OBJ);
$result = $db->fetchAssoc(
' SELECT bug_id, bug_description, bug_status FROM bugs'
);

Continuing from the official website document, this is to fetch the record mode as Object, and then fetch, everything seems natural now, but it is still Use Zend_Db as a black box.
First, look at the code summary of zend/Db.php:
Copy the codeThe code is as follows:

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())
{
//使用Zend_Config对象,上述方式没有使用,直接使用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 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;
}
}

最上方的注释非常值得看,它清楚的说明了这个工厂,另外一段比较重要的几段代码(忽略其中的异常处理)是:
复制代码 代码如下:

//factory有一个参数叫做$adapter
public static function factory($adapter, $config = array())

//确定namespace
$adapterNamespace = 'Zend_Db_Adapter';

//用namespace和上面传入的$adapter构造类名
$adapterName = $adapterNamespace . '_';
$adapterName .= str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($adapter))));

//用上面生成的类名new出obj,看起来PHP比java方便那么一点点哈(Class.forName(‘XXX').newInstance())
$dbAdapter = new $adapterName($config);

Recall the above place where Zend_Db::factory was used to generate $db:
Copy the code The 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 that $adapter is Pdo_Mysql, remember here is Pdo_Mysql , jump again, according to the above $adapterNamespace = 'Zend_Db_Adapter'; you can see that the generated value of $dbAdapter must eventually be: Zend_Db_Adapter_Pdo_Mysql, ok, according to this name, find the zend/db/adapter/pdo directory, ha, so There are so many familiar faces. I saw familiar old faces such as MySql, Mssql, and Sqlite.


Note, note, there is also a low-key Abstract.php inside, which contains 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. Looking at the classes in several other files, they all inherit from Zend_Db_Adapter_Pdo_Abstract. If you want to draw a class diagram, you should have a class diagram like the following:

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

A very, very pure simple factory comes out like this (isn’t it like a simple factory class diagram? That’s just because the position of the class is not placed good).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325670.htmlTechArticleI used ZF some time ago and it didn’t feel very good to treat it as a black box. I always had the urge to look at its source code. but. . . If you look at it bit by bit, the challenge is indeed quite big. One day I thought again...
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
What are the advantages of using a database to store sessions?What are the advantages of using a database to store sessions?Apr 24, 2025 am 12:16 AM

The main advantages of using database storage sessions include persistence, scalability, and security. 1. Persistence: Even if the server restarts, the session data can remain unchanged. 2. Scalability: Applicable to distributed systems, ensuring that session data is synchronized between multiple servers. 3. Security: The database provides encrypted storage to protect sensitive information.

How do you implement custom session handling in PHP?How do you implement custom session handling in PHP?Apr 24, 2025 am 12:16 AM

Implementing custom session processing in PHP can be done by implementing the SessionHandlerInterface interface. The specific steps include: 1) Creating a class that implements SessionHandlerInterface, such as CustomSessionHandler; 2) Rewriting methods in the interface (such as open, close, read, write, destroy, gc) to define the life cycle and storage method of session data; 3) Register a custom session processor in a PHP script and start the session. This allows data to be stored in media such as MySQL and Redis to improve performance, security and scalability.

What is a session ID?What is a session ID?Apr 24, 2025 am 12:13 AM

SessionID is a mechanism used in web applications to track user session status. 1. It is a randomly generated string used to maintain user's identity information during multiple interactions between the user and the server. 2. The server generates and sends it to the client through cookies or URL parameters to help identify and associate these requests in multiple requests of the user. 3. Generation usually uses random algorithms to ensure uniqueness and unpredictability. 4. In actual development, in-memory databases such as Redis can be used to store session data to improve performance and security.

How do you handle sessions in a stateless environment (e.g., API)?How do you handle sessions in a stateless environment (e.g., API)?Apr 24, 2025 am 12:12 AM

Managing sessions in stateless environments such as APIs can be achieved by using JWT or cookies. 1. JWT is suitable for statelessness and scalability, but it is large in size when it comes to big data. 2.Cookies are more traditional and easy to implement, but they need to be configured with caution to ensure security.

How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?How can you protect against Cross-Site Scripting (XSS) attacks related to sessions?Apr 23, 2025 am 12:16 AM

To protect the application from session-related XSS attacks, the following measures are required: 1. Set the HttpOnly and Secure flags to protect the session cookies. 2. Export codes for all user inputs. 3. Implement content security policy (CSP) to limit script sources. Through these policies, session-related XSS attacks can be effectively protected and user data can be ensured.

How can you optimize PHP session performance?How can you optimize PHP session performance?Apr 23, 2025 am 12:13 AM

Methods to optimize PHP session performance include: 1. Delay session start, 2. Use database to store sessions, 3. Compress session data, 4. Manage session life cycle, and 5. Implement session sharing. These strategies can significantly improve the efficiency of applications in high concurrency environments.

What is the session.gc_maxlifetime configuration setting?What is the session.gc_maxlifetime configuration setting?Apr 23, 2025 am 12:10 AM

Thesession.gc_maxlifetimesettinginPHPdeterminesthelifespanofsessiondata,setinseconds.1)It'sconfiguredinphp.iniorviaini_set().2)Abalanceisneededtoavoidperformanceissuesandunexpectedlogouts.3)PHP'sgarbagecollectionisprobabilistic,influencedbygc_probabi

How do you configure the session name in PHP?How do you configure the session name in PHP?Apr 23, 2025 am 12:08 AM

In PHP, you can use the session_name() function to configure the session name. The specific steps are as follows: 1. Use the session_name() function to set the session name, such as session_name("my_session"). 2. After setting the session name, call session_start() to start the session. Configuring session names can avoid session data conflicts between multiple applications and enhance security, but pay attention to the uniqueness, security, length and setting timing of session names.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools