set('db.host', 'localhost') ->set('db.username', 'user') ->set('db.password', 'password') ->set('db.database', 'my_database'); echo $config->get('db.host'); //Output: localhost echo $config->get('db.username'); //Output: user ?>
load('my_config.json', 'json'); echo $config->get('db.host'); //Output: localhost echo $config->get('db.username'); //Output: user ?>
validate([ 'db.host' => ['required'], 'db.username' => ['required'], 'db.password' => ['required'], 'db.database' => ['required'] ]); if (!$config->isValid()) { throw new \Exception('Invalid configuration settings.'); } // Continue with application logic ?>In this example, we use the `validate` method to ensure that required configuration settings are present. We then use the `isValid` method to check if all required settings are present. If any required settings are missing, an exception is thrown. The package library for PHP Object Config is "phpconfig/phpconfig".