Using a variable from another file for use inside a PHP class

I have database credential variables from a file named config.php:

$db_server = 'localhost';
$db_user = 'username';
$db_password = 'secret'
$db_name = 'dbname';

I now have a PHP class in the / class folder, and it works fine for the CRUD process. named MysqlCrud.class.php:

class Database {

    private $db_host = 'localhost';  // Change as required
    private $db_user = 'username';  // Change as required
    private $db_pass = 'secret';  // Change as required
    private $db_name = 'dbname';    // Change as required

}

but I want to use centralized variables from config.php. so I add some lines like this:

include('../config.php');
class Database {

    global $db_server;
    global $db_user;
    global $db_password;
    global $db_name;

    private $db_host = $db_server;  // Change as required
    private $db_user = $db_user;  // Change as required
    private $db_pass = $db_password;  // Change as required
    private $db_name = $db_name;    // Change as required

}

but I got the following error message:

Parse error: syntax error, unexpected 'global' (T_GLOBAL), expecting function (T_FUNCTION) in /home/*** on line **

Why can't I use the variables from the config.php file inside the database class? what did I do wrong? thanks.

+4
source share
4 answers

, , , . , Database, .

:

database.php

class Database {

  private $host;
  private $db_name;
  private $username;
  private $password;

  function __construct($host, $db_name, $username, $password) {
    $this->host = $host;
    $this->db_name = $db_name;
    $this->username = $username;
    $this->password = $password;
  }
}

Database:

include('../config.php');

$db = new Database($db_server, $db_name, $db_user, $db_password);
+3

. , . .

class Database {
    private $db_host;
    //... the rest of them here

    //class constructor. Gets called every time you create an instance of this class.
    function __construct() {
        global $db_server;
        global $db_user;
        global $db_password;
        global $db_name;

        $this->db_host = $db_server;
        //.... you get the idea
    }
}

2017-07-11:

. . , . , global . @br3nt. - $db .

Apache, , -, mod_env . :

<VirtualHost *:80>
    .....
    SetEnv DB_USER=myDatabaseUser
    SetEnv DB_PASSWORD=12345
    ...
</VietualHost>

PHP getEnv('DB_USER') http://php.net/manual/en/function.getenv.php

- config :
config.php

<?php
return [
    'db_user'=>'myDbUser,
    'db_password'=>'12345'
];

, .

Config.class.php

<?php
class Config {
    private $_config;
    public function __construct() {
        $this->_config = include('path/to/config.php');
    }

    public function __get($key) {
        if(isset($this->_config[$key])) {
            return $this->_config[$key];
        }
        return null;
    }
}

:

$config = new Config();
$dbUser = $config->db_user;
$dbPassword = $config->db_password;

2,

?

, , ? , ? . , , . - :

if($myGlobalVariable='something') { ... }

- , . , $myGlobalVariable. , , .

. . , . .

, . , . , PHP, - , .

+3

, :

function connDB()
{
    $conn=mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("database") or die(mysql_error()); 
    return $conn;
};

, globalFunctions.php( ). , .

+3

__construct()

include('../config.php');

class Foo {
    private $db_host;
    private $db_user;
    private $db_pass;
    private $db_name;

    public function __construct() {
        global $db_server;
        global $db_user;
        global $db_password;
        global $db_name;

        $this->db_host = &$db_server;    // Change as required
        $this->db_user = &$db_user;      // Change as required
        $this->db_pass = &$db_password;  // Change as required
        $this->db_name = &$db_name;      // Change as required
    }
}

Assignment by Reference, , ( ).

Also, if you want to avoid writing global, you can use a reserved variable $GLOBALS, which, as described in the documentation , is as follows:

An associative array containing references to all the variables that are currently defined in the global scope of the script. Variable names are array keys.

So your code might look something like this:

$GLOBALS = array(
    'DB_HOST' => 'localhost',
    'DB_USER' => 'user',
    'DB_PASS' => 'secret',
    'DB_NAME' => 'my_db'
);

class Foo {
    private $db_host;
    private $db_user;
    private $db_pass;
    private $db_name;

    public function __construct() {
        $this->db_host = &$GLOBALS['DB_HOST'];  // Change as required
        $this->db_user = &$GLOBALS['DB_USER'];  // Change as required
        $this->db_pass = &$GLOBALS['DB_PASS'];  // Change as required
        $this->db_name = &$GLOBALS['DB_NAME'];  // Change as required
    }
}
+1
source

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


All Articles