Welcome Guest, Not a member yet? Register   Sign In
Define/include "globally available" arrays and functions
#1

(This post was last modified: 05-31-2021, 06:40 AM by tomsenner.)

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
Reply
#2

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/helper...o_currency
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
Reply
#3

@tomsenner,

You could declare CONSTANTS:

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

<?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;
Reply
#5

PHP Code:
$this->config config('yourConfig'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(This post was last modified: 06-06-2021, 02:33 AM by tomsenner.)

(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/helper...o_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?
Reply
#7

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

Sms_helper. Php
Helper(sms)

Call in fuction helper thats it
Reply
#8

(This post was last modified: 06-06-2021, 05:52 AM by paulbalandan.)

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',
]; 
Reply
#9

@paulbalandan
Missing quotes around CURRENCIES.

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

SEE post #3
Reply
#10

(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 ?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB