我目前正在努力将我的 WIP PHP 应用程序转换为面向对象的架构,因为我发现对于我当前的项目来说,良好的 OOP 实践可能会使其变得更加容易。在重构我的代码时,我遇到了一个有点基本的问题,但可惜我不确定答案。
我有一段代码(又名“片段”)——第一个代码示例的“GenerateDBSetObjects()”函数中包含的代码——我觉得应该将其放入一个函数中(即有点像子例程),如第一个示例中所述。我想将其放入单独的功能 block 中有两个原因:
- 简化主要代码体
- 创建一个可以进行单元测试的函数
但是,这会产生一个问题。因为我的程序实际上有两个大范围变量,所以我需要一次返回两个值(这没什么大不了的,因为它是一个常见主题: see this )。我的问题是:由于我正在以面向对象的方式重组我的代码,是否有更有效的方法来做到这一点?也许是我没有考虑到的事情?或者最好只是简单地传入并返回变量?
因为 $NumDBSets 和 $DBSets[] 基本上是全局范围,不太确定我应该在这里做什么。
index.php
之后
//-------------------------Primary Vars---------------------------------------//
//Fills $ini with a multi-dimensional, associative array that contains all of the
// parameters listed in DBSearchConfig.ini
$ini = (parse_ini_file("config/DBSearchConfig.ini", true))
or die ("Config file: 'DBSearchCongif.ini' could not be read or found in the config folder. Please contact the system administrator");
$LogFile = $ini['SystemVars']['LogFile']; //Assign $LogFile to the location of the system's specific log file found in the .ini
$NumDBSets = 0;//An integer which stores the number of Database sets used by the program.
$DBSets = array(); //Creates an empty array that will store each of the DatabaseSet Objects. Each of the
//Database Sets holds an array of SQL database connection parameters (ie.
//Hostname, Username, etc.), as well as an array of links to the SQL databases within the dataset, et. al.
//For more info see 'DatabaseSet.php'
$CurrDBSetNum = $ini['SystemVars']['DefaultDBSet']; //Get the current DBSet Number from config.
$CurrentConnectionManager = new ConnectionManager;
GenerateDBSetObjects($DBSets, $NumDBSets);
//-------------------------FUNCTIONS----------------------------------------//
function GenerateDBSetObjects(){
//Create, Fill and Assign DatabaseSet Objects. Figure out the number of Database sets.
array_push($DBSets, new DatabaseSet);//Push an empty DatabaseSet object into the list to occupy the '0' index!!!
foreach($ini['Databases'] as $ConnectInfoList){
$NumDBSets ++;
//Create a new DatabaseSet Object for this DB Set!!
$newDBSetObject = new DatabaseSet;
$newDBSetObject->ConnectionInfoList = $ConnectInfoList;
$newDBSetObject->CalculateDBSetFields();
array_push($DBSets, $newDBSetObject);
}
}
VS。
之前
//-------------------------Primary Vars---------------------------------------//
//Fills $ini with a multi-dimensional, associative array that contains all of the
// parameters listed in DBSearchConfig.ini
$ini = (parse_ini_file("config/DBSearchConfig.ini", true))
or die ("Config file: 'DBSearchCongif.ini' could not be read or found in the config folder. Please contact the system administrator");
$LogFile = $ini['SystemVars']['LogFile']; //Assign $LogFile to the location of the system's specific log file found in the .ini
$NumDBSets = 0;//An integer which stores the number of Database sets used by the program.
$DBSets = array(); //Creates an empty array that will store each of the DatabaseSet Objects. Each of the
//Database Sets holds an array of SQL database connection parameters (ie.
//Hostname, Username, etc.), as well as an array of links to the SQL databases within the dataset, et. al.
//For more info see 'DatabaseSet.php'
$CurrDBSetNum = $ini['SystemVars']['DefaultDBSet']; //Get the current DBSet Number from config.
$CurrentConnectionManager = new ConnectionManager;
//Create, Fill and Assign DatabaseSet Objects. Figure out the number of Database sets.
array_push($DBSets, new DatabaseSet);//Push an empty DatabaseSet object into the list to occupy the '0' index!!!
foreach($ini['Databases'] as $ConnectInfoList){
$NumDBSets ++;
//Create a new DatabaseSet Object for this DB Set!!
$newDBSetObject = new DatabaseSet;
$newDBSetObject->ConnectionInfoList = $ConnectInfoList;
$newDBSetObject->CalculateDBSetFields();
array_push($DBSets, $newDBSetObject);
}
最佳答案
如果您决定采用 OOP 方法 - 考虑创建一个类来负责生成和存储 DatabaseSet
对象。
如果DatabaseSets生成需要ConnectionManager
类的对象,则将其标记为依赖注入(inject)。
DatabaseSet
类应在单独的文件中声明:DatabaseSet.php
。
让我们调用我们的关键类DatabaseSetAdapter
:
require_once("DatabaseSet.php");
class DatabaseSetAdapter
{
private $config;
private $logFile;
private $NumDBSets = 0;
private $DBSets = [];
private $connManager;
private $currDBSetNum;
public function __construct($iniFilePath, ConnectionManager $manager)
{
$this->config = (parse_ini_file($iniFilePath, true))
or die ("Config file: 'DBSearchCongif.ini' could not be read or found in the config folder. Please contact the system administrator");
$this->logFile = $this->config['SystemVars']['LogFile'];
$this->connManager = $manager;
$this->currDBSetNum = $this->config['SystemVars']['DefaultDBSet'];
}
public function generateDBSetObjects()
{
//Create, Fill and Assign DatabaseSet Objects. Figure out the number of Database sets.
$this->DBSets[] = new DatabaseSet; //Push an empty DatabaseSet object into the list to occupy the '0' index!!!
foreach($this->config['Databases'] as $connectInfoList){
//Create a new DatabaseSet Object for this DB Set!!
$newDBSetObject = new DatabaseSet;
$newDBSetObject->ConnectionInfoList = $connectInfoList;
$newDBSetObject->CalculateDBSetFields();
$this->DBSets[] = $newDBSetObject;
$this->NumDBSets++;
}
}
public function getNumDBSets() // a privileged method
{
return $this->NumDBSets;
}
}
// using of DatabaseSetAdapter:
$dbsetAdapter = new DatabaseSetAdapter("config/DBSearchConfig.ini", new ConnectionManager);
$dbsetAdapter->generateDBSetObjects();
$numDbSets = $dbsetAdapter->getNumDBSets();
....
关于PHP:我应该传入并返回这些变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35229835/