note 129222 added to language.constants

From: Date: Sun, 04 Feb 2024 09:42:03 +0000
Subject: note 129222 added to language.constants
Groups: php.notes 
Request: Send a blank email to php-notes+get-221176@lists.php.net to get a copy of this message
Application settings are something that should be available in multiple places in you application. An excellent use-case for globals. Getting the settings in a global <?php ?php declare(strict_types=1); if(!defined('ROOT')) { define('ROOT', $_SERVER['DOCUMENT_ROOT'] . '/'); } if( !defined( 'SETTINGS_FILE' ) ) { define( 'SETTINGS_FILE', ROOT. 'settings.ini' ); } if( !defined( 'SETTINGS' ) ) { define( 'SETTINGS', parse_ini_file( SETTINGS_FILE, true ) ); } ?> If a proper settings.ini file is provide it makes access to for instance a database connection easy <?php $dsn = SETTINGS['db']['dsn'] ?? 'mysql:host=' . SETTINGS['db']['hostname'] . ';dbname=' . SETTINGS['db']['database']; try { $this->connection = new \PDO( $dsn, SETTINGS['db']['username'], SETTINGS['db']['password'], [ \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8", \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION ] ); } catch ( \PDOException $e ) { throw new DatabaseException( DatabaseException::ERR_CONNECTION, null, $e->getMessage() ); } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.157.134 (proxied: 83.97.87.43) ---- Manual Page -- https://php.net/manual/en/language.constants.php Edit -- https://main.php.net/note/edit/129222 Del: integrated -- https://main.php.net/note/delete/129222/integrated Del: useless -- https://main.php.net/note/delete/129222/useless Del: bad code -- https://main.php.net/note/delete/129222/bad+code Del: spam -- https://main.php.net/note/delete/129222/spam Del: non-english -- https://main.php.net/note/delete/129222/non-english Del: in docs -- https://main.php.net/note/delete/129222/in+docs Del: other reasons-- https://main.php.net/note/delete/129222 Reject -- https://main.php.net/note/reject/129222 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#221176) next »