2

I have a class file Settings.php that loads an ini file and assigns the content to a constant for global access and reducing clutter. The class file contains the logic for loading the settings and define the constant (sorry for the large sample didn't see a way to compress this:

<?php declare(strict_types=1);
namespace Utils;
if( !defined( SETTINGS ) ) {
 if( !defined( 'SETTINGS_FILE' ) ) {
 define( 'SETTINGS_FILE', $_SERVER[ 'DOCUMENT_ROOT' ] . '/settings.ini' );
 }
 class Settings
 {
 public static function getSettings(): array
 {
 return parse_ini_file( SETTINGS_FILE, true );
 }
 }
 define( 'SETTINGS', Settings::getSettings() );
}

With the composer auto loader included the Settings class gets loaded with \Utils\Settings-> getSettings(); and as side effect the SETTINGS is available with the content of the settings.ini file or another one if specified in SETTINGS_FILE.

This works and something like SETTINGS['db']['username'] is available which is a nice compact and highly self-documenting access to the settings. I wonder however if this code smells in any shape or form and if there is a better way to do this.

asked Jun 13, 2023 at 14:17

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.