S
S
Sergey Melnikov2015-10-14 12:59:25
PHP
Sergey Melnikov, 2015-10-14 12:59:25

Is it right to create an instance of a class from a method of this class?

For example, I need to add 100 records to a table. In the controller, I make an instance of class A and call its method, this method will create 100 instances of this class, in a loop. Is that the right thing to do?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Kravchenko, 2015-10-14
@mydearfriend

Something similar to the factory pattern.
But in general, queries to the database in a loop are not the best practice.

A
Andrew, 2015-10-14
@R0dger

It is better to form SQL in a cycle and already with 1 query and insert all 100 at once into a transaction

M
Mandor, 2015-10-14
@Mandor

Fine, but it's better to mark the method as static.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question