// config.php $config = array( 'database' => array( 'host' => 'localhost', 'username' => 'myusername', 'password' => 'mypassword', 'dbname' => 'mydatabase' ), 'debug' => true, 'cache' => array( 'enabled' => true, 'lifetime' => 3600, 'path' => '/var/cache/' ) ); // index.php require_once 'config.php'; if ($config['debug']) { ini_set('display_errors', 1); error_reporting(E_ALL); } $db = new PDO("mysql:host={$config['database']['host']};dbname={$config['database']['dbname']}", $config['database']['username'], $config['database']['password']); if ($config['cache']['enabled']) { $cache = new Cache($config['cache']['path']); $data = $cache->get('mydata'); if (!$data) { $data = fetchDataFromDatabase(); $cache->set('mydata', $data, $config['cache']['lifetime']); } } else { $data = fetchDataFromDatabase(); }
// config.php class Config { private $data; public function __construct() { $this->data = array( 'database' => array( 'host' => 'localhost', 'username' => 'myusername', 'password' => 'mypassword', 'dbname' => 'mydatabase' ), 'debug' => true, 'cache' => array( 'enabled' => true, 'lifetime' => 3600, 'path' => '/var/cache/' ) ); } public function get($key) { return isset($this->data[$key]) ? $this->data[$key] : null; } public function set($key, $value) { $this->data[$key] = $value; } } // index.php require_once 'config.php'; $config = new Config(); if ($config->get('debug')) { ini_set('display_errors', 1); error_reporting(E_ALL); } $db = new PDO("mysql:host={$config->get('database.host')};dbname={$config->get('database.dbname')}", $config->get('database.username'), $config->get('database.password')); if ($config->get('cache.enabled')) { $cache = new Cache($config->get('cache.path')); $data = $cache->get('mydata'); if (!$data) { $data = fetchDataFromDatabase(); $cache->set('mydata', $data, $config->get('cache.lifetime')); } } else { $data = fetchDataFromDatabase(); }In this example, the configuration settings are stored in a config class that has getter and setter methods to retrieve and modify the settings. The settings are then loaded and used in `index.php`. The settings include database connection details, debugging option, and cache settings. Both of these examples utilize a custom or third-party library to store and manage the configuration settings. The library used is not specified in the code examples.