To start, I want to make it clear that this is an efficiency and "coding preference" issue -- not a logic or syntax problem.
I'm trying to build a class for my database interactions and logic. In this class, I find myself "defining" the same set of variables three times (in [Area 1]
, [Area 2]
and [Area 3]
).
This seems redundant and misleading -- there must be a better way of doing things, right?
Currently, my code looks something like this:
class db_connection {
// [Area 1] define properties
private $db_server;
private $db_user;
private $db_password_file;
private $db_name;
public function __construct() {
// [Area 2] give each property a value based on environment variables
$this->db_server = getenv( 'DB_HOST' );
$this->db_user = getenv( 'DB_USER' );
$this->db_password_file = getenv( 'DB_PASSWORD_FILE' );
$this->db_name = getenv( 'DB_NAME' );
}
// create a function to perform connection logic
public function connect() {
// [Area 3] give each property a "labeling" variable for readability and ease-of-use
$db_server = $this->db_server;
$db_user = $this->db_user;
$db_password_file = $this->db_password_file;
$db_name = $this->db_name;
[...]
Here is my reasoning for "defining" in each of my three "Areas":
[Area 1]
Declaring properties at the top of a class definition is standard practice. Normally, these properties would also be initialized (with values), but this is not possible here because I want to assign these properties non-constant values using getenv()
. See the documentation for more information.
[Area 2]
Following my logic for [Area 1]
, I initialize my properties using my class constructor.
[Area 3]
In order to keep my connection logic clean, I gave each one of my properties a local "labeling" variable. I use these properties a lot more in the rest of my code, so having names like $db_user
vs. $this->db_user
makes things easier and more readable.
This seems like a lot of code for just 4 properties/variables...
Is there a better way of organizing things? What should I be doing differently?
Could defining a constant somewhere, or using the static keyword help?
In your case, if the db_server
, db_user
, db_password_file
would not change dynamically at runtime, you'd better define as a constant
not a property
in order to get more efficient performance.
As you mentioned, they are read from the .env
file through the getenv()
function. Yes, predefined constants or properties are not able to call functions or expressions. If I was you, I would create a connection at the moment of construct.
class DBConnection
{
/**
* Connection object
*/
private $connection;
/**
* Construct
*/
public function __construct()
{
// Create a db connection assign to $this->connection
$this->connection = new PDO(getenv('DB_HOST'), getenv('DB_USER'), ......);
}
/**
* Get all users
*/
public function getUser()
{
return $this->connection->query("SELECT * FROM USER");
}
}
/**
* New an instance of DBConnection
* After that, they are all prepared.
*/
$database = new DBConnection;
/**
* Call that function to get all users.
*/
$database->getUser();