Define/include "globally available" arrays and functions |
Hello,
I'd like to define some arrays and functions that I can access "globally" in all controllers/models/views. For example I need to define an array of available currencies: PHP Code: $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 from everywhere? Thank you very much for your help Tom
For your array of currencies, define a new config file. You can load it anywhere you need it.
PHP Code: // load the file For your functions, if it's only simple functions, create a helper file. If it's more complicated and you need a class, maybe create a library. Also, have a look at the number_to_currency function in the number helper: http://codeigniter.com/user_guide/helper...o_currency
<?php namespace MyConfig
use CodeIgniter\Config\BaseConfig; class MyConfig extends BaseConfig { Public $array=[] } To call it create new instance of myconfig $mc = new MyConfig() ; $mc->$array;
Enlightenment Is Freedom
PHP Code: $this->config = config('yourConfig'); What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(06-05-2021, 11:23 PM)John_Betong Wrote: @tomsenner, (06-05-2021, 03:14 PM)includebeer Wrote: For your array of currencies, define a new config file. You can load it anywhere you need it.Thanks a lot for the info. Instead of creating a new helper file, I could also use app/Common.php, right? It says it's like the Master helper file and loaded already anyway. Any disadvantage of using this file?
Helper is fine you call fuction in backend and in html code too
Sms_helper. Php Helper(sms) Call in fuction helper thats it
Enlightenment Is Freedom
You can use the new Files autoloading available to the Autoloader as of v4.1.2. Create a class containing your global functions and defining your currencies as constant.
PHP Code: // example filename: my_global_functions.php After creating the file, write the path of the file in your app/Config/Autoload.php file, particularly in the $files property. PHP Code: // app/Config/Autoload.php
@paulbalandan
Missing quotes around CURRENCIES. define(CURRENCIES, array('EUR', 'USD')); SEE post #3 (06-06-2021, 03:38 AM)paulbalandan Wrote: You can use the new Files autoloading available to the Autoloader as of v4.1.2. Create a class containing your global functions and defining your currencies as constant. This is great - thank you so much. Where would be the best place to store the new file? In app/helpers ? |
Welcome Guest, Not a member yet? Register Sign In |