从一个类到另一个类的变量



我不是很精通PHP。

我在两个不同的文件中有两个类。

class_Functions.php

<?php
class Functions {
[...]
public static function Get_Config($section,$key) {
$config_file = 'config/config.ini';
if (isset($config_data)) {
unset($config_data);
}
$config_data = parse_ini_file($config_file, true, INI_SCANNER_RAW);
return $config_data[$section][$key];
}
[...]
}
?>

class_PDO.php

<?php
Class Connection {
private $server = "mysql:host=XXX;port=YYY;dbname=ZZZ";
private $user = "AAA";
private $pass = "BBB";
private $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
protected $con;
public function openConnection() {
try {
$this->con = new PDO($this->server, $this->user,$this->pass,$this->options);
return $this->con;
} catch (PDOException $e) {
return null;
}
}
public function closeConnection() {
$this->con = null;
}
}
?>

我需要将第二类中的XXX, YYY, ZZZ, AAA和BBB替换为值如下的变量:

XXX→$XXX = Functions::Get_Config('DB', 'host');

YYY→$YYY = Functions::Get_Config('DB', 'port');

打鼾声→美元打鼾声=功能::Get_Config("数据库"、"db_name");

AAA→AAA美元=功能::Get_Config("数据库"、"用户名");

BBB→BBB美元=功能::Get_Config("数据库","密码");

我已经修复了编辑第二个类的问题,如下所示:

Class Connection {
protected $con;
public function openConnection() {
try {
$this->server = "mysql:host=".Functions::Get_Config('DB','host').";port=".Functions::Get_Config('DB', 'port').";dbname=".Functions::Get_Config('DB', 'name');
$this->user = Functions::Get_Config('DB', 'username');
$this->pass = Functions::Get_Config('DB', 'password');
$this->options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$this->con = new PDO($this->server, $this->user, $this->pass, $this->options);
return $this->con;
} catch (PDOException $e) {
return null;
}
}
public function closeConnection() {
$this->con = null;
}
}

最新更新