O
O
Onatskyi2018-06-09 13:02:09
PHP
Onatskyi, 2018-06-09 13:02:09

Database Singleton or Dependency Injection?

Actually, which is better to use? Both options work, but which one is better? Thanks in advance.
PS There is a Database class (select, update, etc.). And there is a User class that uses methods from the Database class.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
davidnum95, 2018-06-09
@davidnum95

DI, for tests

T
ThunderCat, 2018-06-09
@ThunderCat

ST is generally recognized as an anti-pattern in PHP, firstly we do not test it, and secondly it is static.
In the case of a database, it will also not allow you to connect to more than 1 database / storage.
If religion does not allow DI, use registry, this is also badly tested, but in any case there will be no static database object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question