Benefits of parsing the PHP factory model

Source: Internet
Author: User
Parse the benefits of the PHP factory model. As the name implies, the factory can process parts, and the factory mode in the PHP program also has the same function. it is convenient to use a static factory method to instantiate a class. as the name implies, the factory can process parts. Factory modelThere are also the same functions to facilitate the use of a static factory method to instantiate a class. What are the advantages of doing so? My understanding of PHP design patterns is as follows:
In general, when instantiating a class, we will give it some parameters so that we can provide the expected results according to different parameters during its analysis.
For example, the following is a User class, which is very simple:

 _name = $name;      $this->_age = (int)$age;   }   function getName(){      return $this->_name;   }  function getAge(){      return $this->_age;   }}?>

If we want to instantiate this class, we need to do this:
$ U = new User ('xiaoming ', 19 );
Generally, if this class is rarely used, it will have no significant impact and will be very good.
Suddenly I want to add a category for this class, and put James into the student group. it is very easy to modify the class code, however, if this class is instantiated multiple times in many file locations before we want to modify it, it will be very cumbersome to add a parameter to it, because we need to replace it:
$ U = new User ('xiaoming ', 19, 'Student ');
Of course, we can also set the default value in the _ construct function to avoid this repetitive work, but in fact this is not good from the code elegance perspective, suppose we have a factory method that can correspond to a set of parameters through an identifier, and store this parameter in a text document or directly in the factory class as an array, when we call the User class, it will become much easier. even if we need to increase or decrease the parameter attributes, we do not need to replace code everywhere, the following is a factory class (you can also directly store the method in the User class)

Interface IUser {function getName (); function getAge ();} class User implements IUser {protected $ _ group; protected $ _ name; protected $ _ age; function _ construct ($ name, $ age, $ group) {$ this-> _ group = $ group; $ this-> _ name = $ name; $ this-> _ age = (int) $ age;} function getName () {return $ this-> _ name;} function getAge () {return $ this-> _ age;} class Fuser {private static $ group = array ('xiaoming ', 19, 'studen'), array ('Wang ', 19, 'Student '); static function create ($ id) {list ($ name, $ age, $ group) = self: $ group [(int) $ id]; return new User ($ name, $ age, $ group) ;}} echo Fuser: create (0)-> getName ();

The result should be "James ".

Related articles:

Sample code in three forms of PHP factory mode

Analysis on php factory model

Php factory mode

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.