'Laravel - Diference betwen static methods and static instance() method
I am seeing this practice in many Laravel applications, and I don't know the diferences o the reasons to use this.
Instead of having public static methods in the class, many people code only one static function that retrieves an instance of a class an this instace call the function.
This classes have no properties or anything that is needed to make the functions work, so I do not understand why use this way to work. I think this make code more dificult to read, in many cases.
Example, I see this laravel implementantion:
class MyClass
{
public static function instance()
{
return new MyClass();
}
public function myStuff()
{
//my stuff
}
}
//Calling it
MyClass::instance()->myStuff();
I think it would be more readable and same result making all it with static functions, like:
class MyClass
{
public static function myStuff()
{
//my stuff
}
}
//Calling it
MyClass::myStuff();
What do you think about this?
Solution 1:[1]
We know about your concern. It is difficult to discuss what is better, depend your project, your style, your business. But we can give you a keyword to read more about that: Singleton Pattern
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
