Answer the question
In order to leave comments, you need to log in
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
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..
}
$uploader = new Uploader();
$uploader->upload(...);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question