Should we initialize the class object inside the configuration file?

Is it good practice to initialize an object for an authentication class that performs various actions such as a login user, login user, etc. inside the configuration file?

The configuration file basically does things like setting a username, password and site name, etc.

Please indicate the reason if you do not find this standard practice. Just to let you know that most of the code is procedural, with the exception of this particular class.

    // MySQL connection settings
    $db_host = "localhost";
    $db_user = "someuser";
    $db_pass = "some pass";
    $db_name = "somedb";



    // Name of the site
    $site_name = "MyDomainName";   



    $auth = new auth();//initialize Authentication class
$auth->set_auth();//Check if user autorized
+3
source share
4 answers

-, . , . bootstrap.php, , , , .

index.php:

include('bootstrap.php');

bootstrap.php:

include('config.php');
$auth = new auth();
$auth->set_auth();

config.php:

$db_host = "localhost";
$db_user = "someuser";
$db_pass = "some pass";
$db_name = "somedb";
+3

, , register/edit/delete. config , , .

, .

+2

. "" " " . -, (, ) , . ( ) , . -, , "", . new auth() , "" Config .

" ", Config, .. , . , ( PHP). , .ini yaml, , , .

+2

, , : . KISS, . : . , , , .

, . : , ? . (/). , ? , , , : . , .

, , , . , , . , , , , . .

Secondly, the quality code is basically the document itself . Say I have to work in your code, I don’t know that including a configuration file really does authentication. I can spend minutes, maybe hours, trying to understand why a session is created when, looking at the code, nothing indicates this.

In your example, I would use something like this:

<?php

include 'config.php'; // contains function that returns config object/array
include 'auth.php';   // contains functions for authentication

// determine environment, based on server variables (ip, script path, etc)
$environment = getEnvironment($_SERVER);

// get config based on environment
$config = getConfig($environment);

// see if authentication is needed
if (needsAuthentication()) {

    // perform authentication based on request variables (eg: submitted form fields)
    $auth = new auth();
    $auth->doAuth($config, $_REQUEST);
}
+2
source

All Articles