Answer the question
In order to leave comments, you need to log in
What is the disadvantage of using static functions in a project for architecture?
I develop in php (Laravel). It is very convenient for me to create static functions in the model that would retrieve data from the database depending on the input parameters. what is wrong with this approach? in all services where there is a call to the database, I simply call these static methods and get samples.
Answer the question
In order to leave comments, you need to log in
In general, using statistical functions is not bad, if you understand their purpose and use them correctly in simple things like Helper. But there is no need to turn the whole project into a statistical beast.
Objects encapsulate state and behavior, and an aggregate function doesn't.
What the PHP documentation says about static functions:
Declaring the properties and methods of a class as static allows you to access them without creating an instance of the class. They can also be accessed statically in the instantiated object of the class.
$this
is not available inside static methods. So an important reason you should avoid static methods is that using them loses one of the benefits of objects. Objects are designed to encapsulate data. This prevents unexpected side effects that avoid bugs and improves testing. Static methods do not have data encapsulated and therefore do not benefit from this. The only bad thing is not knowing when to use which pattern. You do not know.
There is no problem if you don't store state/data in a static class.
But you need to look to the future: 1) Transactions
may appear that call several different methods at once and you need to store the connection somewhere (so that you don’t connect in each method). (which is very common)
2) You may need several implementations for each DBMS (which is very rare)
3) ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question