Is it wrong for me to use a static method to retune a collection of objects?

14

First of all, I know that I should not go out doing classes with several static methods but in this case I bring here, I do not understand why it can be considered bad practice.

Let's say I have an MVC structure, in my model, I have all of the respective methods of my CRUD, create, read, update, and delete. However, I always do a static method called list, which in turn can return a collection (array same in the case in php) with several objects of the specific model and there, for each object, I have their CRUD methods. I do not see a problem in listing being static, since it will not interact directly with anything else in my class.

I researched a lot on the subject and found nothing that would change my mind, so I wanted to bring this discussion here.

    
asked by anonymous 15.08.2017 / 02:13

2 answers

1

Assuming that your reason is to handle multiple entries in different instances, this makes sense, which is even applied to Laravel with the Route method, which is used to receive instances of other classes, and may have several responsibilities, as in this example:

Path: link

Route::get('produtos/{id}/', function (Request $request, Produtos $produtos) {
   //
});

In the example the Route method calls Static or Get method by manipulating the Request and strong>, these classes could have several other responsibilities within them like the Controllers of the MVC standard, but this, if misapplied, can lead to a problem that is encapsulation of your code, be costly.

Proceeding with the answer still using Laravel as an example, in this case it fits into the 'design pattern #

References:

Laravel Facades Laravel Routing

Default Facade

Related :

    
22.03.2018 / 04:10
1

Today's development is based on several project patterns, and some of these patterns (or all, I do not know) use dependency injections ie inject a class.

I also suggest to implement interfaces in your applications as they help improve the code and putting everything together makes it easier to maintain later.

I recommend you give a studied Design Patterns will help you to evolve a lot as a developer.

    
08.09.2018 / 12:04