Is it good or bad to store the DB connection details with define()?

oasis

Is it good or bad to store the DB connection details with define()?

define( 'DB_HOST', 'localhost' );
define( 'DB_USER', 'root' );
define( 'DB_PASS', 'xxx' );
define( 'DB_NAME', 'xxx' ); 

or better in an array below?

$config['db'] = array(
    'driver'   => 'mysql',
    'host'     => 'localhost',
    'port'     => '3306',
    'user'     => 'someuser',
    'password' => 'SuperSecretPassword',
    'name'     => 'db_name',
);

I notice that popular frameworks such as Zend or Symfony don't use define for this configuration.

CodeIgniter,

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
...

What are the benefits of storing it in an array instead of define()? I notice that Zend appears does not use define() at all (I might be wrong as I am new to Zend). Is it bad to store somme global contants then in general?

In my limited knowledge, I do find storing some global contants useful, for instance, I can store my document root in this below then I can access it from everywhere - by just using WEBSITE_DOCROOT

define ( 'WEBSITE_DOCROOT', str_replace( '\\', '/', dirname( __FILE__ ) ).'/' );

Unlike this method below that I have to use dirname(__FILE__) everytime,

// Load config file
$configFile = dirname(__FILE__) . '/../share/config/default.php';

which I could just use WEBSITE_DOCROOT . '/../share/config/default.php'; to call the file. Isn't it easier and consistent?

Mostafa Lavaei

There is no limitation to use define instead of array. Frameworks use arrays for database connections functions because they want to have some optional parameters like encoding. another reason is there is no needs to define these consts and save memory for them while you just need them once(at bootstrap.php). the idea of define is accessing const everywhere, but for database configurations you don't need it.

*it is better to store database configurations in a file with read-only and not executable permission for apache and deny all access for other users. so this file could not be a php file. you can use xml, ini or other standard formats

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Where to store the DB connection details for a JSP code

From Dev

Store details of a row in an SQLite DB

From Dev

Manually define db connection in laravel

From Dev

Is it ok to define PDO db connection in function?

From Dev

Is it good to use Graph DB to store products catalogue like information?

From Dev

Ruby on Rails: Bad connection with PG after running rake:db migrate

From Dev

Ruby on Rails: Bad connection with PG after running rake:db migrate

From Dev

What is idiomatic way to store DB connection data in an EJB?

From Dev

Using Firebase User UIDs for keys of related DB entries - Good or Bad Practice?

From Dev

Is circular dependency good or bad

From Dev

Static - Good or Bad?

From Dev

Good in HTML, bad in mPDF

From Dev

is #define PFUser good practice

From Dev

Is it a good idea to use global variables to store DB handles in a Go web application?

From Dev

Using ElasticSearch as alternative data store with applications updating both the DB and ES(with the help of Kafka). Is this a good idea?

From Dev

Checking a good connection in Android

From Dev

Is it good or not have connection between it()?

From Dev

define db value while creating connection with redis into nginx.conf file while using Openresty

From Dev

Composite Primary Keys : is it good or bad

From Dev

Is there an indexed list in Haskell and is it good or bad?

From Dev

Rapidly instantiate objects - good or bad?

From Dev

Is getOrCreate function a good or a bad practice?

From Dev

Good or bad practice? Redirecting to stderr?

From Dev

Bad Connection to YQL Query

From Dev

Is it a good practice to define expectation in dataProvider

From Dev

Define connection strings in sharepoint

From Dev

Is using #define considered "bad practice"?

From Dev

How to pass connection details in Celery

From Dev

Server Side program connection details

Related Related

  1. 1

    Where to store the DB connection details for a JSP code

  2. 2

    Store details of a row in an SQLite DB

  3. 3

    Manually define db connection in laravel

  4. 4

    Is it ok to define PDO db connection in function?

  5. 5

    Is it good to use Graph DB to store products catalogue like information?

  6. 6

    Ruby on Rails: Bad connection with PG after running rake:db migrate

  7. 7

    Ruby on Rails: Bad connection with PG after running rake:db migrate

  8. 8

    What is idiomatic way to store DB connection data in an EJB?

  9. 9

    Using Firebase User UIDs for keys of related DB entries - Good or Bad Practice?

  10. 10

    Is circular dependency good or bad

  11. 11

    Static - Good or Bad?

  12. 12

    Good in HTML, bad in mPDF

  13. 13

    is #define PFUser good practice

  14. 14

    Is it a good idea to use global variables to store DB handles in a Go web application?

  15. 15

    Using ElasticSearch as alternative data store with applications updating both the DB and ES(with the help of Kafka). Is this a good idea?

  16. 16

    Checking a good connection in Android

  17. 17

    Is it good or not have connection between it()?

  18. 18

    define db value while creating connection with redis into nginx.conf file while using Openresty

  19. 19

    Composite Primary Keys : is it good or bad

  20. 20

    Is there an indexed list in Haskell and is it good or bad?

  21. 21

    Rapidly instantiate objects - good or bad?

  22. 22

    Is getOrCreate function a good or a bad practice?

  23. 23

    Good or bad practice? Redirecting to stderr?

  24. 24

    Bad Connection to YQL Query

  25. 25

    Is it a good practice to define expectation in dataProvider

  26. 26

    Define connection strings in sharepoint

  27. 27

    Is using #define considered "bad practice"?

  28. 28

    How to pass connection details in Celery

  29. 29

    Server Side program connection details

HotTag

Archive