2016-02-05 48 views
2

我目前正在將我的WIP PHP應用程序轉換爲面向對象的體系結構,因爲我發現對於我當前的項目而言,良好的OOP實踐可能會使它更容易。在重構我的代碼時,我遇到了一個有點基本的問題,但是我不確定答案。PHP:我應該傳入並返回這些變量嗎?

我有一段代碼(也就是'snippet')代碼 - 包含在第一個代碼示例的「GenerateDBSetObjects()」函數中的代碼 - 我覺得應該放入一個函數中像一個子程序),如第一個示例中所述。我希望把它變成一個獨立的功能塊有兩個原因:

  1. 簡化主代碼體
  2. 創建可單元的功能測試

然而,這產生了一個問題。因爲我的程序實際上有兩個大範圍變量,所以我需要一次返回兩個值(這沒什麼大不了的,因爲它是一個常見主題: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

如果你已經決定採取面向對象的方法 - 考慮創建一個將負責生成和存儲對象的類。
如果ConnectionManager類的對象是DatabaseSets生成所必需的,請將其標記爲依賴注入。
類別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(); 
.... 
+0

感謝您的幫助!我花了一些時間閱讀,思考了解你在說什麼。我不知道你的依賴注入意味着什麼(可能是因爲我缺乏正式的培訓)。對於那些與依賴注入類似的人來說,這可能有所幫助:https://en.wikipedia.org/wiki/Dependency_injection –

+0

@BrandonSnider,不客氣!很高興聽到您已經學習了關於依賴注入和PHP中的OOP – RomanPerekhrest

+0

針對我之前的評論,對於那些正在尋找依賴注入的人,或者像我這樣掙扎的人:這有助於熟悉關於[butunclebob.com](http://butunclebob.com/)的關於課堂設計的固體原則和包裝原則,「OOD原則」似乎是一個很好的資源。這也不是一個可怕的資源,但它更專門針對C#/ .net開發人員:[link](http://www.codeproject.com/Articles/22769/Introduction-to-Object-Oriented-Programming-Concep #identify) –

1

全球範圍和OOP不適合toghether。

你應該有一個對象保持此信息

class DBSets { 
    private $num; 
    private $data; 

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

    //setters and getters for accessing private properties 
} 

如果$ NUM只存儲在$數據元素的數量,那麼你可以將其刪除,並使用

count($this->data); 
+0

你可能要考慮辛格爾頓保持它獨特的或使一切靜態 –

+0

感謝米哈伊,我的問題是:這是合適的?感覺DBSets類只是一個存儲數據的數據存儲類。我讀過的其實並不是最好的做法(記住我的經驗略有限制)。我認爲一個單身類是有道理的,但不是更好地調用單身之外的東西而不是DBSets?在我的程序的上下文中,DatabaseSet(aka DBSet)類已經存在。而這些變量實際上只是爲了跟蹤這些對象及其數量。我會考慮這個,因爲這是一個有效的選擇。 –

+0

名稱只是一個例子。不管你想要什麼,你都應該命名它們對象用於存儲數據和處理這些數據的方法。 –

相關問題