This article introduces one of the new things in php5, namely the factory model. Here are some notes about my common factory models, we will share with you the following information for your reference.
In large systems, many codes depend on a few key classes. It may be difficult to change these classes. For example, assume that you have a User class to read from a file. You want to change it to other classes read from the database, but all code references the original class read from the file. At this time, it is very convenient to use the factory model.
Factory mode is a type that provides methods for creating objects for you. You can use the factory class to create objects instead of using new directly. In this way, if you want to change the type of the created object, you only need to change the factory. All codes used in the factory are automatically changed.
Example 1: display a display column of the factory class.
The server side of the equation consists of a database and a set of PHP pages that allow you to add feedback, request feedback lists, and obtain articles related to specific feedback.
The Code is as follows: |
Copy code |
<? Php Interface IUser { Function getName (); } Class User implements IUser { Public function _ construct ($ id ){} Public function getName () { Return "Jack "; } } Class UserFactory { Public static function Create ($ id) { Return new User ($ id ); } } $ Uo = UserFactory: Create (1 ); Echo ($ uo-> getName (). "n "); ?> |
The IUser interface defines what operations a user object should perform. The implementation of IUser is called User, and the UserFactory class creates an IUser object. This relationship can be expressed in UML in Figure 1.
Figure 1. factory class and related IUser interface and user class
If you use the php interpreter to run this code on the command line, the following result is displayed:
% Php factory1.php
Jack
%
The test code will request the User object to the factory and output the result of the getName method.
There is a variation in the factory model that uses the factory method. Class. This method is useful when you create an object of this type. For example, assume that you need to create an object first and then set many attributes. The factory mode of this version encapsulates the process in a single location, so that you do not need to copy complicated initialization code or paste the copied code everywhere in the code base.
Example 2 shows an example using the factory method.
The Code is as follows: |
Copy code |
<? Php Interface IUser { Function getName (); } Class User implements IUser { Public static function Load ($ id) { Return new User ($ id ); } Public static function Create () { Return new User (null ); } Public function _ construct ($ id ){} Public function getName () { Return "Jack "; } } $ Uo = User: Load (1 ); Echo ($ uo-> getName (). "n "); ?> |
Okay, I 've talked a lot about it above. Let's take a look at an instance.
Create the following four files:
Index. php
The Code is as follows: |
Copy code |
<? Php Include_once ("f. inc. php "); $ F = new factory; $ T1 = & $ f-> create ('t1 '); Echo $ t1-> getName (); Echo $ config; ?> |
F. inc. php
The Code is as follows: |
Copy code |
<? Php Class factory { Function factory () { $ This-> mClasses = array ('t1' => 'T1. inc. php', 't2' => 't2. inc. php '); } Function & create ($ class) { If (! Class_exists ($ class )) { Require_once ($ this-> mClasses [$ class]); } Return new $ class; } } ?> |
T1.inc. php
The Code is as follows: |
Copy code |
<? Php Global $ config; $ Config = 'surfchen '; Class T1 { Var $ mName = 'name: t1 '; Function getName () { Return $ this-> mName; } } ?> |
T2.inc. php
The Code is as follows: |
Copy code |
<? Php Class T2 { Function T2 () { Echo 't2 is OK '; } } ?> |
In index. php, we use a factory class to create other class instances.
In the factory, an array $ this-> mClasses is saved in the format of array ("Class Name" => "class file path ").
When we create a class instance through factory: create (), in create (), we first check whether the class exists. If it does not exist, the class file corresponding to the class is included according to $ this-> mClasses. Create and return an instance of this class.
In this way, we only need to include the factory class file in the executed script (such as index. php.
You may also notice the two lines of code in t1.inc. php.
The Code is as follows: |
Copy code |
Global $ config; $ Config = 'surfchen '; |
Why do we need global? Because t1.inc. php is included in factory: create, the variables in t1 file will be the function-level variables of create by default. Therefore, we need to perform global operations on the variables (such as $ config) so that index. php can access them.
Run index. php and the output will be
The Code is as follows: |
Copy code |
Name: T1surfchen |