0

I have a PHP file named DBSettings.php with this information

<?php
    $host="localhost";
    $username="root";
    $password="";
    $db_name="DataBase_new";
    $SideBarCondition="1";
?>

I have another php file where I include this php file

<?php
 include('C:\xampp\htdocs\DBSettings.php');
class DbConnect {
    
    public $con;
    
    function __construct() {
        
        define ("DB_HOST", $host);
        define ("DB_USER", "User");
        define ("DB_PASSWORD", "aasfafdf!34!");
        define ("DB_DATABASE", "DB_new");
        define ("DB_PREFIX", "");
    }
}

but when I execute this I get the error

Notice: Undefined variable: host in C:\xampp\htdocs\project\editorpanel\common\main.php on line 9

I get the error on the line where I use the define ("DB_HOST", $host);. I have already defined the host variable in the DBSettings.php and have included that php file in my current file. So why am I getting this error?

  • Please read [Variable scopes](https://www.php.net/manual/en/language.variables.scope.php) in PHP manual. – Mohd Alomar Feb 02 '21 at 04:02
  • The scope where you include the the DBsettings file is outside the scope where you try to use the variables it sets. – Tangentially Perpendicular Feb 02 '21 at 04:12
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Nico Haase Feb 02 '21 at 15:02

2 Answers2

0

You cannot call $host variable like that. You can pass the variable as a constructor parameter and then assign it.

<?php
 include('C:\xampp\htdocs\DBSettings.php');
class DbConnect {
    
    public $con;
    
    function __construct($host) {
        
        define ("DB_HOST", $host);
        define ("DB_USER", "User");
        define ("DB_PASSWORD", "aasfafdf!34!");
        define ("DB_DATABASE", "DB_new");
        define ("DB_PREFIX", "");
    }
}

$dbConnect = new DbConnect($host);

See php variable scope

Anisur Rahman
  • 644
  • 1
  • 4
  • 17
  • can I use it the include statement inside the class or any other way by which I can fetch the variables inside the class? am asking because I have used DbConnect class in multiple files. it will be tedious work in changing it in every occurance – SAEO Uchiha Feb 02 '21 at 05:55
  • @SAEOTWL At this point, is there any reason why settings are in a separate file? You could simply hardcode those values inside `DbConnect`. – El_Vanja Feb 02 '21 at 10:08
0

I got my answer. I wanted to use the variables inside DBSettings.php inside my current php file. Currently, I was unable to fetch them because I was trying to fetch them inside the class it was out of scope. I fixed it by using the include statement in this position.

class DbConnect {
    
    public $con;
    
    function __construct() {
        include('C:\xampp\htdocs\localdbsettings.php');
        define ("DB_HOST", $host);
        define ("DB_USER", "User");
        define ("DB_PASSWORD", "aasfafdf!34!");
        define ("DB_DATABASE", "DB_new");
        define ("DB_PREFIX", "");
    }
}