PHP:我应该传入和return这些变量吗?

PHP: Should I pass in and return these variables?

我目前正致力于将我的 WIP PHP 应用程序转换为面向对象的体系结构,因为我发现对于我当前的项目,良好的 OOP 实践可能会使它变得更加容易。在稍微重构我的代码时,我遇到了一个有点初级的问题,但遗憾的是我不确定答案。

我有一段代码(又名 'snippet')——第一个代码示例的 "GenerateDBSetObjects()" 函数中包含的代码——我觉得应该将其放入函数 (即有点像子程序),如第一个示例中所述。我想把它放到一个单独的功能块中有两个原因:

  1. 简化主要代码体
  2. 创建一个可以进行单元测试的函数

但是,这会产生一个问题。因为我的程序实际上有两个大范围变量,所以我需要一次 return 两个值(这没什么大不了的,因为这是一个常见的话题:see this)。我的问题是:由于我正在以面向对象的方式重构我的代码,是否有更有效的方法来做到这一点?也许我没有考虑过什么?还是最好简单地传入并 return 变量?

因为 $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 不相适应。

你应该有一个对象来保存这个信息

class DBSets {
  private $num;
  private $data;

  function __construct() {
    $this->num = 0;
    $this->data = [];
  }

  //setters and getters for accessing private properties
}

如果$num只存储了$data中的元素个数,那你可以去掉它,使用

count($this->data);

如果您决定采用 OOP 方法 - 考虑创建一个 class,它将负责生成和存储 DatabaseSet 个对象。
如果 DatabaseSets 生成需要 ConnectionManager class 的对象,将其标记为依赖注入。
Class DatabaseSet 应在单独的文件中声明:DatabaseSet.php.
让我们称我们的关键 class 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();
....