Define/include "globally available" arrays and functions |
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.
@tomsenner ,
Quote: Thank you. Is it ok to declare arrays as a constant? Was the file intended for that? 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-cod...es-all.csv
(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.
06-06-2021, 09:00 AM
(This post was last modified: 06-06-2021, 09:16 AM by tomsenner. Edit Reason: php markup ) (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?
Use the full path:
PHP Code: public $files = [ ROOTPATH . 'app/own_functions.php' ];
06-06-2021, 11:34 AM
(This post was last modified: 06-06-2021, 11:35 AM by tomsenner. Edit Reason: spelling ) (06-06-2021, 11:08 AM)paulbalandan Wrote: Use the full path: 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?
Can you share your functions file, then your view file consuming the function, and the controller consuming the view?
app/_own_vars_functions.php
PHP Code: <?php app/Controllers/Test.php PHP Code: <?php app/Views/Home/index.php PHP Code: <?= $this->extend("_layouts/default") ?> app/Config/Autoload.php PHP Code: <?php
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.
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") ?> |
Welcome Guest, Not a member yet? Register Sign In |