Skip to content
PHP Logo

How can one efficiently manage configuration settings in PHP projects, and what are the benefits of using a dedicated class like "Config" with "get" and "set" methods?

Managing configuration settings in PHP projects can be efficiently done by creating a dedicated class like "Config" with "get" and "set" methods. This allows for centralized management of configuration values, making it easier to update and retrieve settings throughout the project. By encapsulating configuration logic in a separate class, it also promotes code organization and reusability.

php
class Config {
    private $settings = [];

    public function set($key, $value) {
        $this->settings[$key] = $value;
    }

    public function get($key) {
        return isset($this->settings[$key]) ? $this->settings[$key] : null;
    }
}

// Example usage
$config = new Config();
$config->set('db_host', 'localhost');
$config->set('db_user', 'root');

echo $config->get('db_host'); // Output: localhost
echo $config->get('db_user'); // Output: root