我目前正在將我的WIP PHP應用程序轉換爲面向對象的體系結構,因爲我發現對於我當前的項目而言,良好的OOP實踐可能會使它更容易。在重構我的代碼時,我遇到了一個有點基本的問題,但是我不確定答案。PHP:我應該傳入並返回這些變量嗎?
我有一段代碼(也就是'snippet')代碼 - 包含在第一個代碼示例的「GenerateDBSetObjects()」函數中的代碼 - 我覺得應該放入一個函數中像一個子程序),如第一個示例中所述。我希望把它變成一個獨立的功能塊有兩個原因:
- 簡化主代碼體
- 創建可單元的功能測試
然而,這產生了一個問題。因爲我的程序實際上有兩個大範圍變量,所以我需要一次返回兩個值(這沒什麼大不了的,因爲它是一個常見主題: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);
}
感謝您的幫助!我花了一些時間閱讀,思考了解你在說什麼。我不知道你的依賴注入意味着什麼(可能是因爲我缺乏正式的培訓)。對於那些與依賴注入類似的人來說,這可能有所幫助:https://en.wikipedia.org/wiki/Dependency_injection –
@BrandonSnider,不客氣!很高興聽到您已經學習了關於依賴注入和PHP中的OOP – RomanPerekhrest
針對我之前的評論,對於那些正在尋找依賴注入的人,或者像我這樣掙扎的人:這有助於熟悉關於[butunclebob.com](http://butunclebob.com/)的關於課堂設計的固體原則和包裝原則,「OOD原則」似乎是一個很好的資源。這也不是一個可怕的資源,但它更專門針對C#/ .net開發人員:[link](http://www.codeproject.com/Articles/22769/Introduction-to-Object-Oriented-Programming-Concep #identify) –