function - PHP: Should I pass in and return these variables? -
i'm working on converting wip php application object oriented architecture, i've found current project oop practices make easier. while refactoring code bit came upon question elementary, alas i'm not sure of answer.
i have section (aka 'snippet') of code--the code contained in "generatedbsetobjects()" function of first code sample--that feel should put function (ie. subroutine), outlined in first sample. want put separate function block 2 reasons:
- simplify main code body
- create function can unit tested
however, creates problem. because program has 2 large scope variables, need return 2 values @ once (which no big deal, common topic: see this). question have though is: since restructuring code in object-oriented fashion, might there more efficient way this? maybe haven't considered? or best pass in , return variables?
because $numdbsets , $dbsets[] global scope not sure should here.
index.php
after
//-------------------------primary vars---------------------------------------// //fills $ini multi-dimensional, associative array contains of // parameters listed in dbsearchconfig.ini $ini = (parse_ini_file("config/dbsearchconfig.ini", true)) or die ("config file: 'dbsearchcongif.ini' not read or found in config folder. please contact system administrator"); $logfile = $ini['systemvars']['logfile']; //assign $logfile location of system's specific log file found in .ini $numdbsets = 0;//an integer stores number of database sets used program. $dbsets = array(); //creates empty array store each of databaseset objects. each of //database sets holds array of sql database connection parameters (ie. //hostname, username, etc.), array of links sql databases within dataset, et. al. //for more info see 'databaseset.php' $currdbsetnum = $ini['systemvars']['defaultdbset']; //get current dbset number config. $currentconnectionmanager = new connectionmanager; generatedbsetobjects($dbsets, $numdbsets); //-------------------------functions----------------------------------------// function generatedbsetobjects(){ //create, fill , assign databaseset objects. figure out number of database sets. array_push($dbsets, new databaseset);//push empty databaseset object list occupy '0' index!!! foreach($ini['databases'] $connectinfolist){ $numdbsets ++; //create new databaseset object db set!! $newdbsetobject = new databaseset; $newdbsetobject->connectioninfolist = $connectinfolist; $newdbsetobject->calculatedbsetfields(); array_push($dbsets, $newdbsetobject); } }
vs.
before
//-------------------------primary vars---------------------------------------// //fills $ini multi-dimensional, associative array contains of // parameters listed in dbsearchconfig.ini $ini = (parse_ini_file("config/dbsearchconfig.ini", true)) or die ("config file: 'dbsearchcongif.ini' not read or found in config folder. please contact system administrator"); $logfile = $ini['systemvars']['logfile']; //assign $logfile location of system's specific log file found in .ini $numdbsets = 0;//an integer stores number of database sets used program. $dbsets = array(); //creates empty array store each of databaseset objects. each of //database sets holds array of sql database connection parameters (ie. //hostname, username, etc.), array of links sql databases within dataset, et. al. //for more info see 'databaseset.php' $currdbsetnum = $ini['systemvars']['defaultdbset']; //get current dbset number config. $currentconnectionmanager = new connectionmanager; //create, fill , assign databaseset objects. figure out number of database sets. array_push($dbsets, new databaseset);//push empty databaseset object list occupy '0' index!!! foreach($ini['databases'] $connectinfolist){ $numdbsets ++; //create new databaseset object db set!! $newdbsetobject = new databaseset; $newdbsetobject->connectioninfolist = $connectinfolist; $newdbsetobject->calculatedbsetfields(); array_push($dbsets, $newdbsetobject); }
if have decided take approach oop - consider creating class responsible generation , storing databaseset
objects.
if object of connectionmanager
class required databasesets generation, mark dependency injection.
class databaseset
should declared in separate file: databaseset.php
.
let's call our crucial 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' not read or found in config folder. please contact system administrator"); $this->logfile = $this->config['systemvars']['logfile']; $this->connmanager = $manager; $this->currdbsetnum = $this->config['systemvars']['defaultdbset']; } public function generatedbsetobjects() { //create, fill , assign databaseset objects. figure out number of database sets. $this->dbsets[] = new databaseset; //push empty databaseset object list occupy '0' index!!! foreach($this->config['databases'] $connectinfolist){ //create new databaseset object db set!! $newdbsetobject = new databaseset; $newdbsetobject->connectioninfolist = $connectinfolist; $newdbsetobject->calculatedbsetfields(); $this->dbsets[] = $newdbsetobject; $this->numdbsets++; } } public function getnumdbsets() // privileged method { return $this->numdbsets; } } // using of databasesetadapter: $dbsetadapter = new databasesetadapter("config/dbsearchconfig.ini", new connectionmanager); $dbsetadapter->generatedbsetobjects(); $numdbsets = $dbsetadapter->getnumdbsets(); ....
Comments
Post a Comment