通过变量调用设置PHP类变量

时间:2019-06-05 19:09:35

标签: php

我正在修改我购买的脚本,但我无法使Database类中设置的Database name变量成为我在其之上构造的变量的结果。我需要将$ dbName变量设置为附加到从数据库中获取的变量$ username的“ mmo_”的组合。数据库的名称应为“ mmo_school”,school是从数据库中提取的$ username变量。但是,除非我将变量的文本放在$ dbName =之后的单引号中,否则它将失败。但是我需要根据哪个用户正在使用脚本来动态设置数据库名称。我希望这是有道理的,非常感谢您的帮助!

$username = "school";
$newname = "mmo_" . $username;  **// the results of this would be "mmo_school"**

class Database 
{
    private static $dbName = $newname ; 
    private static $dbHost = 'localhost' ;
    private static $dbUsername = 'username';
    private static $dbUserPassword = 'password';

    private static $cont  = null;

    public function __construct() {
        exit('Init function is not allowed');
    }
    public static function connect() {
        // One connection through whole application
        if ( null == self::$cont ) {      
            try {
                self::$cont =  new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword);  
            } catch(PDOException $e) {
                die($e->getMessage());  
            }
        } 
        return self::$cont;
    }

    public static function disconnect() {
        self::$cont = null;
    }
}

如果我输入$ dbName ='mmo_school';这项工作。 如果我尝试使用如上所示创建的变量,就不会!

感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

创建一个公共静态方法来设置主机,用户名,密码等。此外,我会将您的构造方法更改为私有而非公共。

Database::setDB('mmo_' . $username);
// or
Database::setConnectInfo('mmo_' . $username, 'some host', $username, 'some password');
$db = Database::connect();

class Database 
{
    private static $dbName = $newname ; 
    private static $dbHost = 'localhost' ;
    private static $dbUsername = 'username';
    private static $dbUserPassword = 'password';

    private static $cont  = null;

    private function __construct() {
        exit('Init function is not allowed');
    }

    public static function setDB($dbName){
        self::$dbName = $dbName;
    }

    // or use this function 

    public static function setConnectInfo($dbName, $dbHost, $dbUsername, $dbUserPassword){
        self::$dbName = $dbName;
        self::$dbHost = $dbHost;
        self::$dbUsername = $dbUsername;
        self::$dbUserPassword = $dbUserPassword;
    }

    public static function connect() {
        // One connection through whole application
        if ( null == self::$cont ) {      
            try {
                self::$cont =  new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword);  
            } catch(PDOException $e) {
                die($e->getMessage());  
            }
        } 
        return self::$cont;
    }

    public static function disconnect() {
        self::$cont = null;
    }
}

答案 1 :(得分:0)

在您的课堂上,您必须调用$ newname作为全局变量。进行此较小的更改后,您的代码应该可以使用。

$username = "school";
$newname = "mmo_" . $username;  **// the results of this would be "mmo_school"**

class Database {

private static $dbName = "" ; 
private static $dbHost = 'localhost' ;
private static $dbUsername = 'username';
private static $dbUserPassword = 'password';

private static $cont  = null;

function __construct($init_parameter) {
global $newname;
    $this->$dbName = $newname;
}
public static function connect() {
    // One connection through whole application
    if ( null == self::$cont ) {      
        try {
            self::$cont =  new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword);  
        } catch(PDOException $e) {
            die($e->getMessage());  
        }
    } 
    return self::$cont;
}

public static function disconnect() {
    self::$cont = null;
}

}

答案 2 :(得分:0)

因为单引号不进行变量扩展-使用双引号

$var = 'dingo';

$name = 'xyz_$var';   // result 'xyz_$var'
$name = "xyz_$var";   // result 'xyz_dingo'