PHP:我应该传入并返回这些变量吗?

时间:2016-02-05 17:15:31

标签: php function oop refactoring abstraction

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

我有一段代码(又称“代码片段”)代码 - 第一个代码示例的“GenerateDBSetObjects()”函数中包含的代码 - 我觉得应该放入一个函数中(即有点像一个子程序),如第一个样本中所述。我想把它放到一个单独的功能块中有两个原因:

  1. 简化主要代码正文
  2. 创建可以进行单元测试的功能
  3. 但是,这会产生问题。因为我的程序实际上有两个大的范围变量,所以我需要一次返回两个值(这没什么大不了的,因为它是一个常见的主题: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);
    }
    

2 个答案:

答案 0 :(得分:2)

如果您决定采用OOP方法 - 考虑创建一个负责生成和存储DatabaseSet个对象的类。
如果DatabaseSets生成需要ConnectionManager类的对象,请将其标记为依赖注入 类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();
....

答案 1 :(得分:1)

全球范围和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);