W
W
WebDev2015-08-02 13:37:37
Laravel
WebDev, 2015-08-02 13:37:37

Static or non-static method?

Hello, probably a stupid question, but recently I thought about it. I am writing a project in Laravel, in models I use static methods to get data: the most popular products, similar products, etc. And recently, one person asked me: “Why do you use static methods?”, “Why not?”, I answered, because they are at least shorter, you don’t need to create an object. To which he said that it was not comme il faut and not OOP and something else indistinct.
How do you write? What are the pros/cons of static/non-static methods?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dave, 2015-08-02
@kirill-93

Static is a global state. A static method or a static variable is absolutely no different from a global variable, because neither the static nor the global variable was passed as arguments anywhere and in any way, which means they come from the global space.
Imagine that you are writing a utility for loading images. And now the moment has come when you need to specify cropping options for previews. With static it might look like this:

public function upload()
{
     $width = Config::read('width');
     $height = Config::read('height');
     // .. Do upload..
}

Problems with this approach?
1. You need to know and be sure that the static Config class was initialized somewhere far away. What if it wasn't initialized?
2. What if you decide to change the config source? For example, to read, is it all not from the `Config` class, but from somewhere from REST? We'll have to rewrite everything, then test again. This problem is known as a strong tie .
3. Try to write a unit test for this, without hacks and perverted crutches. After all, in order to test this method, we need to initialize `Config` somewhere in the global space, then make sure that it works.
4. Hidden dependencies.
For example, initializing the class, in the case of static:
$uploader = new Uploader();
$uploader->upload(...);

You and your users do not see what dependencies a class has and what it depends on. The problem is especially noticeable when you write a library or a component.
In order to understand the consequences of statics, try using at least one component of the Yii framework separately. For example, if you only need CAPTCHA, you won't be able to pull it out without rewriting almost the entire component, because there is a global state everywhere inside, in the form of `Yii::$app->`. That is, in order to use only captcha, you will have to connect the entire framework and all its internal mechanisms when it is absolutely not necessary.
As for Laravel, there are fewer statics, since some components, like Eloquent, can be used separately. Static in the lara, it exists as a wrapper, but not as an implementation, in many places unlike Yii.
And answering why - because the authors either do not care about crystal clear testability, or a weak link, or because there is not enough experience to write clean and maintainable code.
Almost everywhere , all dependencies are passed as arguments, which is good, loosely coupled, and testable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question