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 more information:
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 more information.
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. 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 instance code is as follows:
- 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 the User object should perform. the implementation of IUser is called User, and the UserFactory class creates the IUser object. this relationship can be expressed in UML.
Figure 1. factory classes and their related IUser interfaces and user classes if you use the php interpreter to run this code on the command line, the following results will be obtained:
% Php factory1.phpJack %
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 if it is very important to 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 of using the factory method.
The instance code is as follows:
- 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:
The index. php instance code is as follows:
- Include_once ("f. inc. php ");
- $ F = new factory;
- $ T1 = & $ f-> create ('T1 ');
- Echo $ t1-> getName ();
- Echo $ config;
- ?>
The code of the f. inc. php instance is as follows:
- 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;
- }
- }
- ?>
The t1.inc. php instance code is as follows:
- Global $ config;
- $ Config = 'surfchen ';
- Class T1
- {
- Var $ mName = 'name: T1 ';
- Function getName ()
- {
- Return $ this-> mName;
- }
- }
- ?>
The t2.inc. php instance code is as follows:
- Class T2
- {
- Function T2 ()
- {
- Echo 'T2 is OK ';
- }
- }
- ?>
In index. in 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. then create and return an instance of this class. in this way, we only need to include the factory class file into the execution script (such as index. php. you may also notice that in t1.inc. these two lines of code in php.
The instance code is as follows:
- Global $ config;
- $ Config = 'surfchen ';
Why do we need global? Because t1.inc. php is included in factory: create. the variables in the t1 file are the function-level variables of create by default. so we need to make global for the variables (such as $ config) so that the index. php can access. run index. php will output
The instance code is as follows:
- Name: T1surfchen