E
E
Eka5552016-03-07 15:18:10
PHP
Eka555, 2016-03-07 15:18:10

How many instances of one class can be created?

There is a class "DataBase" the appeal goes by the method of chaining.
I do it like this:

$db  = DataBase::instance();
$db->table('users')->get();

I am using singleton.
The question is how many instances of class can be created?
Is it possible or even necessary to do this:
$db  = new DataBase;
$db->table('users')->get();

I have almost everything done this way. Here is another example:
$validation  = Validation::instance();
$validation->validate($_POST,$rules);

Please explain where and how to do it.
I want to understand where it is necessary to use Singleton and where it is not necessary.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
trevoga_su, 2016-03-07
@Eka555

do you think there are situations when you need to have two connections in a script to two different databases?
in my opinion, this situation is quite real
, but making a class for working with subd alone is not right

M
Mikhail Osher, 2016-03-07
@miraage

Despite the fact that the singleton is an anti-pattern, it is indispensable in development.
Specifically, in this case, the scheme is as follows:
- the connection to the database is stored in the singleton = DbConnection
- the querybuilder creates new objects itself, but uses the DbConnection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question