CodeIgniter Forums
Proposal to Combine Constants - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: Proposal to Combine Constants (/showthread.php?tid=80249)



Proposal to Combine Constants - John_Betong - 10-06-2021

PHP now allows array Constants which could group current defines:
Code:
// Works as of PHP 7
define('ANIMALS', array(
    'dog',
    'cat',
    'bird'
));
echo ANIMALS[1]; // outputs "cat"



RE: Proposal to Combine Constants - kenjis - 10-06-2021

Sorry, what do you propose?


RE: Proposal to Combine Constants - InsiteFX - 10-06-2021

I think he means to use the new array constants for the defines in CodeIgniter 4

Also maybe the app/Config/Constants.php defines.


RE: Proposal to Combine Constants - paulbalandan - 10-07-2021

That would work if data are meant to be arrays. However, if several items are grouped as arrays for the sake of utilizing array constants, I don't think it is worth the effort.

Imagine combining all the error code constants (EXIT_ERROR, EXIT_AUTO_MIN, EXIT_AUTO_MAX, etc.) to an array constant EXIT_CODES. So which one would you prefer to use the error exit code, EXIT_ERROR or EXIT_CODES[0] ?


RE: Proposal to Combine Constants - John_Betong - 10-08-2021

I have four sites sites (soon to be more) all calling a common CI4 application and find the following ideal for displaying the relevant constants for each domain:

file: /app/Views/incs/QQQ.php
Code:
 
<?php declare(strict_types=1);

$CI_VERSION = \CodeIgniter\CodeIgniter::CI_VERSION ;

define('QQQ', [
  'memory_get_usage()'      => number_format((float)memory_get_usage()) .' bytes',
  'memory_get_peak_usage()' => number_format(memory_get_peak_usage()).' bytes',
  'date("h:i:s")'  => date('l, jS F Y -H:i:s'),
  'base_url()'      => base_url(),
  'CI_DEBUG'        => CI_DEBUG    ? 'TRUE' : 'FALSE', 
  'APPPATH'        => APPPATH,
  'BASEURL'        => BASEURL, 
  'CI_VERSION'      => $CI_VERSION, 
  'ENVIRONMENT'    => ENVIRONMENT, 
'CI_ERROR_LOG'    => CI_ERROR_LOG,
  'FCPATH'          => FCPATH,
  'LOCALHOST'      => LOCALHOST,
  'ROOTPATH'        => ROOTPATH,
  'SYSTEMPATH'      => SYSTEMPATH,
  'TESTPATH'        => TESTPATH,
  'WRITEPATH'      => WRITEPATH,
]);

# LOCALHOST defined in index.php
if(LOCALHOST) :
# Maybe rendered in footer.php
echo tabulate((array) QQQ, $title='Table: QQQ'); 
endif;


I thought the grouping may be useful instead of separate Constants?