从配置文件到另一个类的全局变量

时间:2014-03-23 03:46:27

标签: php oop global-variables

简单的问题。我还是OOP的新手,基本上都是学习基础知识。我有一个config.php文件,下面写着。

<?php
    $hName          =   'localhost';    //  Hostname :]

    $dbName         =   'db';   //  Database
    $tbAdmin        =   'admin';        //  Table administrator
    $tbPosts        =   'posts';        //  Table posts

    $dbUser         =   'phpadmin';         //  Database login uname
    $dbPass         =   'phpadmin';         //  Database login pw
?>

这是我的functions.php文件:

class databaseEstablish {
    public $dbc;

/**
 * Connect to database (make a connection)
 * @return boolean Return true for connected / false for not connected
 */
public function connect() {
        require_once 'config.php';

        $this->host     = $hName;
        $this->username = $dbUser;
        $this->password = $dbPass;
        $this->database = $dbname;

        $this->dbc = @mysqli_connect($this->host, $this->username, $this->password, $this->database);
    } 

虽然这应该可以起作用,但是输出会显示错误消息(点替换路径,另外三个点替换下面的行'require'需要config.php':

Notice: Undefined variable: hName in ... ...

Notice: Undefined variable: dbUser in ... ...

Notice: Undefined variable: dbPass in ... ...

Notice: Undefined variable: dbname in ... ...

<小时/> 我是否必须使用类和公共函数或构造函数来配置文件,如果没有,有什么问题?据我所知,这些是配置文件中的globabl变量,应该可以从任何其他文件访问。感谢。

3 个答案:

答案 0 :(得分:1)

创建config.php文件

define("hName", 'localhost');    //  Hostname :]
define("dbName", 'db');
define("tbPosts", 'posts');
define("dbUser", 'phpadmin');
define("dbPass", 'phpadmin');

创建class.php

     require_once 'path_to/config.php';
class databaseEstablish {
    public $dbc;

/**
 * Connect to database (make a connection)
 * @return boolean Return true for connected / false for not connected
 */
public function connect() {




        $this->dbc = @mysqli_connect(hName, dbUser, dbPass, dbname);
    } 

答案 1 :(得分:0)

当您添加require_once时,它将会像这样呈现..

public function connect() {
        $hName          =   'localhost';    //  Hostname :]
        $dbName         =   'db';   //  Database
        $tbAdmin        =   'admin';        //  Table administrator
        $tbPosts        =   'posts';        //  Table posts

        $dbUser         =   'phpadmin';         //  Database login uname
        $dbPass         =   'phpadmin'; 

        $this->host     = $hName;
        $this->username = $dbUser;
        $this->password = $dbPass;
        $this->database = $dbname;

这是错误的实际上是 ....而是在类之外添加require_once并将参数传递给函数..

修改后的代码......

<?php

require_once 'config.php';
class databaseEstablish {
    public $dbc;

    /**
     * Connect to database (make a connection)
     * @return boolean Return true for connected / false for not connected
     */
    public function connect($hName,$dbUser,$dbPass,$dbname) {


        $this->host     = $hName;
        $this->username = $dbUser;
        $this->password = $dbPass;
        $this->database = $dbname;

        $this->dbc = mysqli_connect($this->host, $this->username, $this->password, $this->database);
    } 
}

$dbEst = new databaseEstablish($hName,$dbUser,$dbPass,$dbname);

答案 2 :(得分:0)

你的要求应该是这样的:

require_once('config.php');

最好将配置定义为常量。例如:

define('hName','localhost');
define('dbUser', 'username');

并像这样使用它:

@mysqli_connect(hName,dbUser...

等等。我建议不要把@放在任何事情面前。 它将抑制调用该方法时产生的任何错误。由于您是新手,因此您不应忽略错误报告。错误报告对于对代码进行故障排除非常重要。