Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Dependency-injection of functions

Hi,

Yesterday I'd asked a question about migrating from CodeIgniter to Phalcon. I'm still working through that and trying to write up my experiences :)

Anyway, in CodeIgniter you can have helpers, which allow you to define a function outside of the scope of a class.

I use these for some of the more complicated validation I do. For example, I have a check_int function which checks to see if something is an integer OR can be interpretted as one - say the string "15", which isn't an integer but, if parsed correctly, can be interpretted as one.

My question is, is this possible to use the Phalcon dependency injector to load these functions if required? I mean, I can always do a require_once or something like that, but was wondering if there was a proper way to do it



44.9k
Accepted
answer

If you put the functions in a static namespaced class like Project\Util then you can set that class with its path in the Loader and call it with Project\Util::check_int.

Its easy to add helpers to views but not in such a general way in the application. You could easily extend the loader as the that functionality is a pretty basic wrapper around require_once.