PHP: define constants outside the class or in the constructor?

I'm new to classes and oo. I was looking for a MySQL base class to get started, and I found Matthew Zaragoza's “Simple MySQL Class”.

These are the first lines:

define('SIMPLE_DB_SERVER', 'mysqlserver.net'); define('SIMPLE_DB_NAME', 'mydbname'); define('SIMPLE_DB_USERNAME', 'myusername'); define('SIMPLE_DB_PASSWORD', 'mypassword'); class ASimpleMySQLDB { public function __construct($server, $database, $username, $password){ $this->conn = mysql_connect($server, $username, $password); $this->db = mysql_select_db($database,$this->conn); } [...] 

I wonder if there is a specific reason why constants are defined outside the class instead of using the constructor - an example:

 public function __construct(){ $this->conn = mysql_connect('localhost', 'username', 'password'); $this->db = mysql_select_db('database',$this->conn); } 

Or:

 public function __construct($which_db){ if($which_db=='firstdb'){ $server='localhost'; $username='user1'; $password='pass1'; $database='db1'; }elseif($which_db=='otherdb'){ $server='localhost'; $username='user2'; $password='pass2'; $database='db2'; } $this->conn = mysql_connect($server, $username, $password); $this->db = mysql_select_db($database,$this->conn); } 

Or using a switch or something else.

What is the difference between these two methods? What would you recommend? Thank you very much: -)

+4
source share
5 answers

Although most OO PHP developers are not encouraged these days, many libraries still use define() to set configuration constants that must be modified by the end-party implementing the library. They are usually cleaned up in the configuration file, which is required / included by the class that needs this configuration information, or even included in the additional backup of the stack if it is needed all over the world (Wordpress comes to mind.)

PHP 5 includes class constants to perform the same function, but only in the scope of the class itself.

http://php.net/manual/en/language.oop5.constants.php

This approach is a good way to create class-specific, immutable values ​​that your library will require. Since one of the main goals of using OOP in PHP is to reuse code, this makes it easy to configure for release. In the case of your database class, you could have const declarations prior to your build method defining all access credentials such as db_host, db_user and db_pass. Note that these constants need to be accessed by ClassName::ConstName , as a static method.

Although OOP in PHP has come a long way and is now widespread, a ton of older or more “locked up” frameworks and libraries still use the definition convention, so you'll see that it is very often used even in OO environments.

+2
source
  • This way other files / classes can access a specific value (GLOBAL)
  • Easier to edit if you are going to change your db in the future

The best way to implement this is to create a configuration file with all these definitions. i.e

configuration.php

 define('SIMPLE_DB_SERVER', 'mysqlserver.net'); define('SIMPLE_DB_NAME', 'mydbname'); define('SIMPLE_DB_USERNAME', 'myusername'); define('SIMPLE_DB_PASSWORD', 'mypassword'); 

and include them in your MySQL connection.php connection class

 include "configuration.php"; class DBConnection { public function __construct(){ $this->conn = mysql_connect(SIMPLE_DB_SERVER, SIMPLE_DB_USERNAME, SIMPLE_DB_PASSWORD); $this->db = mysql_select_db(SIMPLE_DB_NAME,$this->conn); } } 
+2
source

I'm not sure about the specific context of this particular MySQL connection class, but the difference between defining connection information as constants rather than class properties is one area.

Constants are available anywhere in the code base after executing this particular line of code, so you can access them outside of the ASimpleMySQLDB class. On the other hand, any variables defined inside the constructor will not work outside the class.

Often I find that people use constants at the beginning of their code to set values ​​that should be changed (usually only once) by the end user (WordPress uses this in his wp-config.php file http: //codex.wordpress. org / Editing_wp-config.php ). Thus, people do not need to delve into the class itself to find where to set the database connection values. I guess this readability factor is why this author decided to use constants.

+1
source

Constants are defined in one place. And in your examples, other developers will have to look for database parameters.

0
source

This separation is most likely due to the fact that the ASimpleMySQLDb class is a wrapper around the database. This is a general class, so to make it specific to the details of hard coding in the constructor, you will make it impossible for you to reuse it. You want to maintain this flexibility even within a single application, as you can get multiple databases.

Personally, I tend to create an Application class that follows the singleton pattern and declares database connection data as static variables in this class. This allows me to still easily change database connection information or add additional databases as needed.

0
source

Source: https://habr.com/ru/post/1380814/


All Articles