I have found this class to connect via PDO on the internet and analyzing it there is something I do not understand. When, in the conditional, you ask if the constant 'environment' is defined and if it is equal to 'development', are you referring to a constant that must have been defined by the programmer before? or are you referring to a predefined constant in php that I don't know? because I have searched for the other files and I have not found anything, and searching in the manual and on the internet either
The funny thing is that l seems to be defined with an error type...
Let's see if you know anything about this
Thank you!
class DbConnect {
private $conn;
function __construct() {
}
function connect() {
include_once dirname(__FILE__) . '/Config.php';
try {
$this->conn = new PDO('mysql:host=' .
DB_HOST.';dbname='.
DB_NAME.';charset=utf8',
DB_USERNAME,
DB_PASSWORD);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $this->conn;
} catch(PDOException $ex) {
// if the environment is development, show error details, otherwise show generic message
if ( (defined('ENVIRONMENT')) && (ENVIRONMENT == 'development') ) {
echo 'An error occured connecting to the database! Details: ' . $ex->getMessage();
} else {
echo 'An error occured connecting to the database!'. $ex->getMessage();
}
exit;
}
}
}
Each extension can define its own constants, and it is good practice to prefix them with an extension-related prefix. It is definitely not a kernel constant, which is usually prefixed with PHP_.
As long as you use a framework and review the documentation, it's clear that the purpose -as far as we can see- is simply to display information to the developer.
When programming as a team, the need for Development Operations (DevOps) arises, where we usually have several environments (computers and/or servers) named according to the stage of development or maturity of the code, to mention a few:
Depending on the needs of the project, you will have two or more, and the code will move unidirectionally from the first to the last.
The mysterious constant is to define that environment, in such a way that it should not be defined in the Productive environment so as not to show information that may be useful to a developer -or hacker- but very inconvenient to a terrestrial user. That is, you can assign a value to the constant a
development
in any development environment that you control, but take care that under no circumstances does this value reach the Production environment, where it will surely have another value or none at all.