php - Declaring properties and variables in an efficient and readable way?


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 usinggetenv(). 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 thedb_server,db_user,db_password_file would not change dynamically at runtime, you'd better define as aconstant not aproperty in order to get more efficient performance.

As you mentioned, they are read from the.env file through thegetenv() 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.



Areas 1 and 2 look unavoidable. They are as they must be to accomplish that task.

Area 3, however, there is a trick you can use. You can easily define all class variables with local equivalents using the following command:


What's happening here is thatget_object_vars() returns an array of all variables defined on a specified object. In this case, we are targeting$this, which is the class instance. It will provide an array of all defined class variables, which then get created as local copies upon passing toextract().

This has the benefit of working cleanly if the values of these class variables need to change. Otherwise, using constants might be prettier.

People are also looking for solutions to the problem: How to enable PHP short tags?


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.