jeudi 12 août 2021

Does it make sense to have singleton service classes in Laravel?

There is a class in my project, for example CalculationService. This class has methods that manipulates data in it's way. This class is injected in many places through application using the Laravel DI. Does it make sense to register it as singleton to avoid multiple instances through the application?

class CalculationService {

}
class CarController extends Controller 
{
    public function __construct(CalculationService $service)
    {
        $this->calculationService = $service;
    }

    public function show() 
    {
        $this->calculationService->multiply();
        ...
    }
}

Aucun commentaire:

Enregistrer un commentaire