An analysis of the benefits of using the PHP factory model

Source: Internet
Author: User
Before we introduce you to the PHP factory model of three, and what is the PHP Factory mode, why use PHP Factory mode, then we will give you a detailed introduction of the PHP factory model of the use of the benefits!

In general, we instantiate a class that gives it some parameters so that when it is structured, it can feed back the results we need based on different parameters.
For example, here is a user class, which is very simple:

The code is as follows:

<?phpinterface iuser{   function GetName ();   function Getage ();} Class User implements iuser{   protected $_name;   protected $_age;   function construct ($name, $age) {      $this->_name = $name;      $this->_age = (int) $age;   }   function GetName () {      return $this->_name;   }  function Getage () {      return $this->_age;   }}? >

If we're going to instantiate this class, it's going to be:
$u = new User (' Xiao Ming ', 19);
In general, if this class is seldom used, it is not very significant and very good.
Suddenly I want to add a collation to this class, put Xiao Ming into the student group, modify the following class code implementation is very easy, but if this class in many file places before we want to modify the instantiation, then want to add a parameter will become very cumbersome, because it needs to replace:
$u = new User (' Xiao Ming ', 19, ' student ');
Of course, we can avoid this duplication by setting the default value in the construct function, but in fact it's not very good from a code-elegant point of view, imagine that we have a factory method that can use an identity to correspond to a set of parameters, And this parameter is stored in a text document or directly in the form of an array in the factory class, we call the user class will be much easier, even if the need to add or subtract parameter properties and do not need to replace the code everywhere, the following is a factory class (you can also directly store the method in the user class)

The code is as follows:

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 (       ' Xiao Ming ', 19, ' student '),       array (' Xiao 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 the output "xiaoming".

Summarize:

I believe that the small partners on the use of PHP Factory mode benefits have a certain understanding, according to the things learned in this article, practice in their own work!

Related recommendations:

Simple introduction to three types of PHP factory models


What is the PHP Factory mode? Why use PHP Factory mode?


Three forms of sample code in 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.