What is a Factory design pattern in PHP?

It bothers me with the simplest words, what does he do? Pretend you are explaining almost everything to your mother or someone.

+75
php factories
Jan 18 '10 at 1:28
source share
8 answers

A factory creates an object. So if you want to build

class A{ public $classb; public $classc; public function __construct($classb, $classc) { $this->classb = $classb; $this->classc = $classc; } } 

You will not want to rely on the need to execute the following code each time you create an object

 $obj = new ClassA(new ClassB, new Class C); 

There will be a factory. We define a factory to take care of this for us:

 class Factory{ public function build() { $classc = $this->buildC(); $classb = $this->buildB(); return $this->buildA($classb, $classc); } public function buildA($classb, $classc) { return new ClassA($classb, $classc); } public function buildB() { return new ClassB; } public function buildC() { return new ClassC; } } 

Now all we have to do is

 $factory = new Factory; $obj = $factory->build(); 

The real advantage is when you want to change the class. Let's say we wanted to pass another ClassC:

 class Factory_New extends Factory{ public function buildC(){ return new ClassD; } } 

or new ClassB:

 class Factory_New2 extends Factory{ public function buildB(){ return new ClassE; } } 

Now we can use inheritance to easily change the way a class is created, to introduce a different set of classes.

A good example would be this user class:

 class User{ public $data; public function __construct($data) { $this->data = $data; } } 

This $data class uses the class we use to store our data. Now for this class, let's say we use a session to store our data. factory will look like this:

 class Factory{ public function build() { $data = $this->buildData(); return $this->buildUser($data); } public function buildData() { return SessionObject(); } public function buildUser($data) { return User($data); } } 

Now, let's say, instead we want to store all our data in a database, it is very simple to change it:

 class Factory_New extends Factory{ public function buildData() { return DatabaseObject(); } } 

Factories is the design pattern that we use to control the union of objects, and using the right factory patterns allows us to create custom objects that we need.

+159
Jan 18
source share

As in a real life factory, he creates something and returns it.

Imagine something like this

 $joe = new Joe(); $joe->say('hello'); 

or factory

 Joe::Factory()->say('hello'); 

The implementation of the factory method will create a new instance and return it.

+15
Jan 18 '10 at 1:31
source share

In the general case, a factory creates something: in the case of an Object-Orientated-Programming template, a factory creates objects.

It does not matter if it is in PHP, C # or any object-oriented language.

+1
Jan 18 '10 at 1:31
source share

Factory The Factory Pattern is designed to loosen communications. Like the factory value, the data for the factory (produce data) to the end user. Thus, the factory breaks the tight connection between the data source and the data process.

+1
Jun 17 '13 at 11:22
source share

A factory simply generates an object or objects.

You may have a factory that creates a MySQL connection.

http://en.wikipedia.org/wiki/Factory_method_pattern

0
Jan 18 '10 at 1:30
source share

This answer refers to another post in which Daniel White said to use factory to create a MySQL connection using the factory pattern.

For a MySQL connection, I would prefer to use a singleton template, since you want to use the same connection to access the database, another is not created.

0
Apr 09 '14 at 13:30
source share

The classic approach to creating an instance of an object:

 $Object=new ClassName(); 

PHP has the ability to dynamically create an object from a variable name using the following syntax:

 $Object=new $classname; 

where the variable $ classname contains the name of the class that it wants to instantiate.

So, the classic factoring of objects will look like this:

 function getInstance($classname) { if($classname==='Customer') { $Object=new Customer(); } elseif($classname==='Product') { $Object=new Product(); } return $Object; } 

and if you call the getInstance ('Product') function, this factory will create and return a Product object. Otherwise, if you call the getInstance ('Customer') function, this factory will create and return an object of type Customer (created from the Customer () class).

There is no need for this, you can send "Product" or "Client" (the exact names of existing classes) as a variable value for dynamically creating an instance:

 $classname='Product'; $Object1=new $classname; //this will instantiate new Product() $classname='Customer'; $Object2=new $classname; //this will instantiate new Customer() 
0
Sep 14 '14 at 12:41
source share

For the record, in light words, a factory, as @Pindatjuh said, returns an object.

So what's the difference with the constructor? (which does the same thing)

  • the constructor uses its own instance.
  • Something I want to do something more advanced, and I do not want to inflate the object (or add dependencies).
  • The constructor is called when each instance is created. Sometimes you don’t want it.

    For example, let's say that every time I create an object of the Account class, I read a file from the database and use it as a template.

Using constructor:

 class Account { var $user; var $pwd; var ... public __construct() { // here i read from the file // and many other stuff } } 

Using factory:

 class Account { var $user; var $pwd; var ... } class AccountFactory { public static Create() { $obj=new Account(); // here we read the file and more stuff. return $obj; } 
0
Mar 26 '16 at 21:33
source share



All Articles