CodeIgniter Forums
Define/include "globally available" arrays and functions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forum-10.html)
+--- Thread: Define/include "globally available" arrays and functions (/thread-79346.html)

Pages: 1 2 3


Define/include "globally available" arrays and functions - tomsenner - 05-31-2021

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


RE: Define/include "globally available" arrays and functions - includebeer - 06-05-2021

For your array of currencies, define a new config file. You can load it anywhere you need it.
PHP Code:
// load the file
$config config('MyConfig');

// get some values...
$config->currencies

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/helpers/number_helper.html#number_to_currency


RE: Define/include "globally available" arrays and functions - John_Betong - 06-05-2021

@tomsenner,

You could declare CONSTANTS:

index.php
Code:
<?php
defined('CURRENCIES') ?: define('CURRENCIES', array("EUR","USD") );
...
...



RE: Define/include "globally available" arrays and functions - paliz - 06-06-2021

<?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;


RE: Define/include "globally available" arrays and functions - InsiteFX - 06-06-2021

PHP Code:
$this->config config('yourConfig'); 



RE: Define/include "globally available" arrays and functions - tomsenner - 06-06-2021

(06-05-2021, 11:23 PM)John_Betong Wrote: @tomsenner,

You could declare CONSTANTS:

index.php
Code:
<?php
defined('CURRENCIES') ?: define('CURRENCIES', array("EUR","USD") );
...
...
Thank you. Is it ok to declare arrays as a constant? Was the file intended for that?
My array might even change at some point if we add more currencies.

(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.
PHP Code:
// load the file
$config config('MyConfig');

// get some values...
$config->currencies

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/helpers/number_helper.html#number_to_currency
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?


RE: Define/include "globally available" arrays and functions - paliz - 06-06-2021

Helper is fine you call fuction in backend and in html code too

Sms_helper. Php
Helper(sms)

Call in fuction helper thats it


RE: Define/include "globally available" arrays and functions - paulbalandan - 06-06-2021

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

define('CURRENCIES', array('EUR''USD'));

if (! 
function_exists('foo'))
{
    function foo()
    {
            // logic here
    }


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

public $files = [
    'path/to/my_global_functions.php',
]; 



RE: Define/include "globally available" arrays and functions - John_Betong - 06-06-2021

@paulbalandan
Missing quotes around CURRENCIES.

define(CURRENCIES, array('EUR', 'USD'));

SEE post #3


RE: Define/include "globally available" arrays and functions - tomsenner - 06-06-2021

(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.

PHP Code:
// example filename: my_global_functions.php

define(CURRENCIES, array('EUR''USD'));

if (! 
function_exists('foo'))
{
    function foo()
    {
            // logic here
    }


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

public $files = [
    'path/to/my_global_functions.php',
]; 

This is great - thank you so much. Where would be the best place to store the new file? In app/helpers ?