How to reuse functions using services

Hello.

I have made a function that I need to call from two different observers, and both in the created and update methods (I have to call it 4 times).

I have tried to have one only function in an functions file and call it from the 4 different places, but I have not succeded. It is just a 10 line function, but there must be a better way to do this either than repeating the code 4 times.

Any ideas of how to call a function which is in an external FUNCTIONS CUSTOM FILE from inside the observers?

I understand that inside the same Observer I can use:
$this->nameOfFunction()

But how to call that from an outer file?
Solution
Sounds like you need a service class or a helper class.
Was this page helpful?