O people, I have a lot of difficulty in understanding this and I was just thinking that someone can help me with some questions. . I feel that there is a code found to create a connection with the pedo, the problem I found was the connection defined within my actions. Someone suggested the world, but then pointed towards a 'better' solution. My questions with this code are:
-
What does the connection factory mean? What gets inside the new connection function (...)
-
When the connection is defined $ db = new PDO (...); Why not try or hold (I use them for handling error)? Does this mean that I will have to try and catch every query later?
Here is the code:
class ConnectionFactory {Private Stable $ factory; Public stable work getFactory () {If (! Auto :: $ factory) auto :: $ factory = new connectionfish (...); Returns self: $ factory; } Private $ db; Public function getConnection () {If (! $ Db) $ db = new PDO (...); Return $ db; }} GetSomething function () {$ conn = ConnectionFactory :: getFactory () - & gt; Mill connections (); . . . You seem to be confused about the subject of design patterns like factory.
Maybe you have a design pattern about PHP or normal pattern Any book or some tutorial should be read about. Just google "php design pattern"
But to answer your question in a nutshell:
-
The use of a connection factory independently produces connection objects Is done to The actual built-in database will handle the assembly of a simple PDO factory DSN connection strings, which are based on the standards your PDO has passed and ready to use the PDO object. In most cases it is useful for creating a more sophisticated database adapters class, which provides an error in settling the problem in a more comfortable manner. . Then, a factory class can then be used to produce the right connection object according to your database system.