Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Developer654079525's avatar

Organizing utils

Where do you keep your user-made utils routines? In a separate class, in models, in a namespace or somewhere else? Do you make them static?

0 likes
3 replies
Tray2's avatar

It depends, if it is for a single model, and single action. then I'd probably stick it in the model. IF it on the other hand is something that for example handles different models, but with similar functionality in multiple methods, then I'd create a service class, and inject it.

This service class converts a text to a primary key, depending on the model.

https://github.com/Tray2/mediabase/blob/main/app/Services/ForeignKeyService.php

And this is an example where it's used.

https://github.com/Tray2/mediabase/blob/main/app/Http/Controllers/Books/BooksStoreController.php

1 like
martinbean's avatar

@developer654079525 In an appropriate file. I also group “utils” into classes with descriptive methods, rather than just showing arbitrary functions into a “helpers” or “utils” file.

A “helper” or “util” is just a method you’ve not thought about enough. Work out what it’s actually doing and that should also point you how to better group it and name it.

Please or to participate in this conversation.