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

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Define/include "globally available" arrays and functions (/showthread.php?tid=79346)

Pages: 1 2 3


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

You can place the file anywhere you want as it is not specifically a helper. Helpers' file names should end in `_helper`, like `number_helper.php` for the `helper()` function to work. Since your file is not a helper per se, anywhere is fine as long as you include the full path to the `$files` property.


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

@tomsenner ,
Quote: 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.

Yes easy to increase and have umpteen new currencies. Also if created in index.php it is globally available same as APPPATH, FCPATH, ROOTPATH, etc

Edit
Why not create a database table from the following CSV file:

https://github.com/datasets/currency-codes/blob/master/data/codes-all.csv


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

(06-06-2021, 02:31 AM)tomsenner Wrote: 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?

Yes absolutely, I forgot about Common.php because I never had to use it yet. The only disadvantage I can think of is if you have a lot of functions and it becomes a mess of thousands of line. Then it would make more sense to have separate helpers for each subject.


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

(06-06-2021, 05:54 AM)paulbalandan Wrote: You can place the file anywhere you want as it is not specifically a helper. Helpers' file names should end in `_helper`, like `number_helper.php` for the `helper()` function to work. Since your file is not a helper per se, anywhere is fine as long as you include the full path to the `$files` property.

I created the file "app/own_functions.php" and added the line 

PHP Code:
public $files = [ 'app/own_functions.php', ]; 

in "app/Config/Autoload.php". But the view that is using the function only outputs the error "Call to undefined function".
I tried to change the path inside public $files in various ways (e.g without "app/" only 'own_functions.php'). Still only errors. What's my mistake?


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

Use the full path:
PHP Code:
public $files = [ ROOTPATH 'app/own_functions.php' ]; 



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

(06-06-2021, 11:08 AM)paulbalandan Wrote: Use the full path:
PHP Code:
public $files = [ ROOTPATH 'app/own_functions.php' ]; 

Unfortunately I am still getting the same error "call to undefined function" within my view file. It is not loading the file. Should that function now be accessible in views, controllers and models?


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

Can you share your functions file, then your view file consuming the function, and the controller consuming the view?


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

app/_own_vars_functions.php
PHP Code:
<?php

function mkTabName($txt){
    $txt explode(".",$txt);
    $txt str_replace("_and_"," &amp; ",$txt[0]);
    return ucFirst(str_replace("_"," ",$txt));
}

?>

app/Controllers/Test.php

PHP Code:
<?php

namespace App\Controllers;

class 
Test extends BaseController
{

    public function 
test()
    {

            return 
view("Home/index.php");

    }



app/Views/Home/index.php
PHP Code:
<?= $this->extend("_layouts/default"?>

<?= $this->section("title"?>Home<?= $this->endSection() ?>

<?= $this->section("test"?>

    <h1>Home</h1>

Test <?= mkTabName("Test.php-name"?>

<?= $this->endSection() ?>

app/Config/Autoload.php
PHP Code:
<?php

namespace Config;

use 
CodeIgniter\Config\AutoloadConfig;

/**
 * -------------------------------------------------------------------
 * AUTO-LOADER
 * -------------------------------------------------------------------
 *
 * This file defines the namespaces and class maps so the Autoloader
 * can find the files as needed.
 *
 * NOTE: If you use an identical key in $psr4 or $classmap, then
 * the values in this file will overwrite the framework's values.
 */
class Autoload extends AutoloadConfig
{
    
/**
     * -------------------------------------------------------------------
     * Namespaces
     * -------------------------------------------------------------------
     * This maps the locations of any namespaces in your application to
     * their location on the file system. These are used by the autoloader
     * to locate files the first time they have been instantiated.
     *
     * The '/app' and '/system' directories are already mapped for you.
     * you may change the name of the 'App' namespace if you wish,
     * but this should be done prior to creating any namespaced classes,
     * else you will need to modify all of those classes for this to work.
     *
     * Prototype:
     *
     *   $psr4 = [
     *       'CodeIgniter' => SYSTEMPATH,
     *       'App'           => APPPATH
     *   ];
     *
     * @var array<string, string>
     */
    
public $psr4 = [
        
APP_NAMESPACE => APPPATH// For custom app namespace
        
'Config'      => APPPATH 'Config',
    ];

    
/**
     * -------------------------------------------------------------------
     * Class Map
     * -------------------------------------------------------------------
     * The class map provides a map of class names and their exact
     * location on the drive. Classes loaded in this manner will have
     * slightly faster performance because they will not have to be
     * searched for within one or more directories as they would if they
     * were being autoloaded through a namespace.
     *
     * Prototype:
     *
     *   $classmap = [
     *       'MyClass'   => '/path/to/class/file.php'
     *   ];
     *
     * @var array<string, string>
     */
    
public $classmap = [];


    public 
$files = [ ROOTPATH 'app/_own_vars_functions.php' ]; 





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

This is working for me (although I have to use another view file as your example is not rendering in me). The global autoloading works fine, no error call to undefined function is emitted.
PHP Code:
<?= mkTabName('Test.php-name'?> // echoes 'Test' 

I think this has something to do with your view file as it is not working for me.


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

Thank you very much for your answer. It's odd that it's working for you. But I doubt it's because of my view file- The view loads fine over here!
Probably doesn't work for you because you don't have the layout setup 
PHP Code:
<?= $this->extend("_layouts/default"?>