Source Code Files of this Chapter
project-root/
├── app/
│ ├── Controllers/
│ │ └── HomeController.php # modified file
│ ├── Core/
│ │ ├── App.php # modified file
│ │ ├── Bootstrap.php # modified file
│ │ ├── Config.php # new file
│ │ ├── Env.php # new file
│ │ └── ExceptionHandler.php # modified file
│ └── Helpers/
│ └── helpers.php # new file
├── config/
│ └── app.php # modified file
└── public/
└── index.php # modified file
Access Environment and Configuration Variable
We have also simplified the task of fixing errors by implementing error handling, but this error debug information cannot be displayed in the production environment, only in dev mode.
But how will the program detect the environment? We will try to detect the environment so that the program can work according to the environment information and various configurations.
So let's start by making sure that error information is only displayed in the dev environment. In addition, you can configure integrations with databases and various third party sites using environment variables.
Create .env File
Create an .env file on the root (/) directory of the project.
APP_NAME=PHP core OOP project
APP_ENV=dev
APP_DEBUG=true
Code Explain
APP_NAME: Name of the application
APP_ENV: Server environment of the application
APP_DEBUG: Application debug mode
Create .env File Parser
Create app/Core/Env.php file to parse the .env file
<?php
declare(strict_types=1);
namespace App\Core;
class Env
{
public static function load(string $path)
{
if (!file_exists($path)) {
throw new \RuntimeException(".env file not found at {$path}");
}
$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || str_starts_with($line, '#')) {
continue;
}
[$name, $value] = array_map('trim', explode('=', $line, 2));
$value = trim($value, "\"'");
putenv("$name=$value");
$_ENV[$name] = $value;
$_SERVER[$name] = $value;
}
}
}
Code Explain
public static function load(string $path)This parser class contains only the single static action method named
load. This method's access modifier ispublicso that we can call it from anywhere without creating an object.if (!file_exists($path)) { throw new \RuntimeException(".env file not found at {$path}"); }If the file path does not exist, return an exception. This is
fail-fast design. It confirms that configuration is available for the application.$lines = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($lines as $line) { $line = trim($line); if ($line === '' || str_starts_with($line, '#')) { continue; }Read each line of the file except comment line (start with #) and store it in an array.
[$name, $value] = array_map('trim', explode('=', $line, 2)); $value = trim($value, "\"'");the array destructuring operator has used to define left portion of the
=(assignment operator) to$namevariable and define right portion of the=(assignment operator) to the$valuevariable by using array destructuring.putenv("$name=$value"); $_ENV[$name] = $value; $_SERVER[$name] = $value;Created environment variable using
putenv(),$_ENV['KEY'],$_SERVER['KEY'].
Create Config File
Environment variables are not standard OOP structured data. So we will use Config class to use environment variable as OOP way throughout the application.
Create config/app.php file:
<?php
return [
'name' => $_ENV['APP_NAME'] ?? 'My OOP App',
'url' => ($_ENV['APP_URL'] ?? 'http://localhost:8000'),
'env' => $_ENV['APP_ENV'] ?? 'local',
'debug' => ($_ENV['APP_DEBUG'] ?? 'false') === 'true',
];
Code Explain
Reads configuration values from environment variables, applies safe default values when they are not set, and returns them as an associative array.
Create Config File Parser
Create app/Core/Config.php file to parse the config file:
<?php
declare(strict_types=1);
namespace App\Core;
class Config
{
protected static array $items = [];
public static function load(string $configPath)
{
foreach (glob($configPath . '/*.php') as $file) {
$key = basename($file, '.php');
self::$items[$key] = require $file;
}
}
public static function get(string $key, mixed $default = null)
{
$keys = explode('.', $key);
$value = self::$items;
foreach ($keys as $k) {
if (!is_array($value) || !array_key_exists($k, $value)) {
return $default;
}
$value = $value[$k];
}
return $value;
}
public static function all()
{
return self::$items;
}
}
Code Explain
As same the
.envparser, each method isstaticand visibility ispublic. So, we will be able to call method directly using the scope resolution operator(::)without create an instance (object) of that class.
load()method uses theglob()to scan all files of theconfigdirectory and load all file’s content to the $items[] array by using therequirestatement.
get()method will provide config value by using(.)dot notation.
all()method will provide all config keys and values as an array.
Using Environment and Configuration Variable
Create a helper function config to the app/Helpers/helpers.phpfile:
<?php
declare(strict_types=1);
use App\Core\Config;
if (!function_exists('config')) {
function config(string $key, mixed $default = null)
{
return Config::get($key, $default);
}
}
Load .env file parser, config file parser and helper files to the Core/Bootstrap.php file:
use App\Core\Env;
use App\Core\Config;
Env::load(__DIR__ . '/../../.env');
Config::load(__DIR__ . '/../../config');
require __DIR__ . '/../Helpers/helpers.php';
See full code:
Core/Bootstrap.php
<?php
declare(strict_types = 1);
use App\Core\Env;
use App\Core\Config;
require __DIR__ . '/../../vendor/autoload.php';
// Create an instance of the ExceptionHandler and register it
$exceptionHandler = new \App\Core\ExceptionHandler();
$exceptionHandler->register();
// 1. Load .env
Env::load(__DIR__ . '/../../.env');
// 2. Load configs
Config::load(__DIR__ . '/../../config');
// 3. Load helpers
require __DIR__ . '/../Helpers/helpers.php';
return new \App\Core\App();
The config() helper function is available in the application. So, replace previous hardcoded configuration value with config().
Open the app/Controllers/HomeController.php and update as like below:
- private $appName;
- public function __construct($appName) {
- $this->appName = $appName;
- }
public function getGreeting()
{
- return 'Welcome to the ' . $this->appName;
+ return 'Welcome to the ' . config('app.name');
}
Code Explain
We are using config() method directly from the controller that’s why the
__construct()method is not required.
See full code
app/Controllers/HomeController.php
<?php
declare(strict_types=1);
namespace App\Controllers;
class HomeController
{
public function getGreeting()
{
return 'Welcome to the ' . config('app.name');
}
}
__construct() method is removed from the app/Controllers/HomeController.php. So, update the app/Core/App.php file as like below:
- // load environment
- $config = [
- 'app_name'=> 'PHP core OOP project'
- ];
// route define
$route = $_SERVER['REQUEST_URI'];
if ($route == "/") {
- $homeController = new HomeController($config['app_name']);
- echo $homeController->getGreeting();
+ $homeController = new HomeController();
+ echo $homeController->getGreeting();
}
See full code:
app/Core/App.php
<?php
declare(strict_types = 1);
namespace App\Core;
use App\Controllers\HomeController;
class App
{
public function run()
{
// route define
$route = $_SERVER['REQUEST_URI'];
if ($route == "/") {
$homeController = new HomeController();
echo $homeController->getGreeting();
}
if ($route == "/get-error") {
// trigger a fatal error
trigger_error("An Intentional Error Occurred.", E_USER_ERROR);
}
if ($route == '/get-exception') {
// throw an exception
throw new \Exception("An Intentional Exception Occurred.");
}
}
}
Display Error Message Only on Dev Server
Use the config() function to show error message conditionally only in the dev server. Update the handleException() method of the app/Core/ExceptionHandler.php file:
// This method will now handle both exceptions and errors
// that have been converted to ErrorExceptions.
http_response_code(500);
+ $appEnv = Config::get('app.env');
+ if ($appEnv == 'dev') {
echo 'Message: ' . $e->getMessage() . '<br>';
echo 'File: ' . $e->getFile() . '<br>';
echo 'Line: ' . $e->getLine() . '<br>';
+ } else {
+ echo 'An Error Occurred';
+ }
/*
* Optional:
* You can log the error message to a log file
* or perform other log related actions
*/
+
+ error_log($e->getMessage());
Result
Make sure server is running or go to the project-root directory and run command php -S localhost:8000 -t public/ on the terminal.
- Browse
http://localhost:8000-> Returns a welcome greeting message. See the app name in the message. It should be same as the Environment variable.
- Browse error and exception pages in both server environment modes
devandproduction.- Browse:
http://localhost:8000/get-error-> Returns error information only indevenvironment.
- Browse:
http://localhost:8000/get-exception-> Returns exception information only indevenvironment.
- Browse:
If the testing results are satisfactory, commit and push your changes to the remote repository.
Important
The .env file will contain sensitive environment variables, so it should not be added to the Git repository. Please update the .gitignore file as shown below:
/vendor
+.env
Top comments (0)