I'd like to define some arrays and functions that I can access "globally" in all controllers/models/views (in codeigniter 4). For example I need to define an array of available currencies:
$currencies = array("EUR","USD");
And some functions e.g. to display numbers differently depending on the users country as some use decimal points or commas.
Where would I put those arrays and functions and how would I include them so they are available within all controllers/models/views?
I researched that you can define constants in app/config/Constants.php but it doesn't seem to be the place for arrays and functions.
I'll try to separate fact from opinion here.
...define some arrays and functions that I can access "globally" in all controllers/models/views...
Use inheritance - You can create 'base' classes that your Controllers and Models extend from; these 'base' classes extend CodeIgniter's Controller or Model, and define any methods you want within all of your Controllers or Models.
Each Controller or Model you create that extends its respective 'base' class will have access to anything (non-private) that you define in the 'base' class.
Use namespacing- In truth, that's the power of it. If you really need something you can access from "anywhere", create a Library or something similar. It really doesn't matter what it is as long as it's within a defined namespace.
Then when you need to use this Library (or whatever), you create an instance and use it. You could even use static members if you didn't want to actually create an object.
Use 'Helpers'- To stay procedural you can both extend the functionality of existing CI4 Helpers or even create your own. You can find other resources on this as well.
...you can define constants in app/config/Constants.php but it doesn't seem to be the place for arrays and functions.
Defining array constants inside Constants.php
is fine so long as you're actually defining a constant; $currencies = array("EUR","USD");
is not a constant.
You're correct about functions in that file, however; you should not do that.
A quick smattering of opinion based on general(ish) consensus:
The purpose of Controllers is to properly route requests; the purpose of Views (broadly speaking) is to render web pages. Neither of these is truly the place to be performing any business logic; hence the catchphrase "fat models, skinny controllers".
If you think you need to access or perform business logic outside of the Models, stop for a second and ask yourself why that isn't occurring in the Model layer instead.