D
D
D3lphi2016-09-07 20:34:27
PHP
D3lphi, 2016-09-07 20:34:27

Is it correct to use Singleton for db class and templating engine?

Good day. I decided to "ride" a little for practice and write a class for working with the database, as well as a small template engine. Googling, I saw that some people write such classes using a singleton. As I understand it, in order to access a single instance of an object from any corner of the application. Do I need to go this way and if not, advise which design patterns to use / not use for these purposes.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Cat Anton, 2016-09-07
@27cm

No.
Read about DI .
Why everything else is bad:
github.com/codedokode/pasta/blob/master/arch/di.md
PHP implementations:
github.com/ziadoz/awesome-php#dependency-injection .

T
trevoga_su, 2016-09-08
@trevoga_su

Is it correct to use Singleton for db class and templating engine?

No, not correctly
connections can be MANY, to different bases.
the template engine object, if it contains values ​​assigned by the user, can have a second instance for processing some independent pieces of code ,
and you also have a class for the database - database.phpinfo.su
ONLY 1 instance of the class. For example, a Request and Response object.

E
evnuh, 2016-09-07
@evnuh

Can.
There is nothing wrong with having a global link to access the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question