oop - Php Separation of Concerns & Dependency Injection -


based on advice gotten here on stackoverflow, need more guidance. told separation of concerns important keeping code neat , modular , finding is.

my question this: based on have read soc, have developed 2 classes. suppliers class , csv class. suppliers merely retrieves data db regarding different suppliers. csv class retrieves data csv file being imported, of info need parse it, end goal of inserting data supplier tables. in order accomplish goal of inserting csv data database, need use methods both classes. make 3rd class called importsupplierscsv or make more sense create import function method of suppliers class?

shortened save space, classes so:

class suppliers {       public $db;     public $inv;     public $table;       public function __construct (pdo $db)     {          $this->db = $db;         $this->inv = 'lightsnh_inventory';         $this->table = 'suppliers';      }       public function getsuppliers()      {                         $sql = 'select * `'.$this->inv.'`.`'.$this->table.'`';         $statement = $this->db->query($sql);         $result = $statement->fetchall(pdo::fetch_assoc);          return $result;      }       public function getactivesuppliers()      {                         $suppliers = $this->getsuppliers();              $active = array();               foreach($suppliers $supplier) {              if($supplier['exclude'] == 0)                                $active[] = $supplier;          }          return $active;      }       public function getdistributors()      {                         $suppliers = $this->getsuppliers();              $distributors = array();                 foreach($suppliers $supplier) {              if($supplier['type'] == 1)                               $distributors[] = $supplier;          }          return $distributors;      }        class csv     {           public $form;           public function __construct($form_name)         {              $this->form = $form_name;          }           public function getfile()         {              if(isset($_post[$this->form.'-upload-submit'])) {                  return $_files[$this->form.'-file'];              }          }           public function getname()         {              $file = $this->getfile();             return $file['name'];          }           public function getextension()         {              return end(explode('.',$this->getname()));          }           public function gettype()         {              $file = $this->getfile();             return $file['type'];          }   etc..... 

neither. oop/soc/di etc. not end, they're means. purpose of soc modular architecture making easier maintenance, although separating concerns enlarges problem obscuring it. found best way come design in php first make work, , refactor. can in imagination first save time. organize way see best. after all, idea whoever going maintain code able find way easily, since organized way. in short, beware of over-engineering.

in example, not bother make class of if going used in 1 place. example: application can import csv records database, has form edit such rows, i'll assume there 'save' function in suppliers class, given abstracts queries (badly - no caching, no clause, sql injection etc). problem of importing csv file problem of presenting data in orderly fashion 'save' function.

now, basic code import csv file database simple this:

$data = array_map( 'str_getcsv', explode("\n", file_get_contents( $filename ) ) ); $columns = array_shift( $data ); $sth = $db->preparestatement( "insert suppliers( " . implode( ',', $columns )   . ") values ( " . substr(str_repeat(",?",count($columns)),1) . ")" ); foreach ( $data $line )   $sth->execute( $line ); 

usually not ideal - in essence core functionality. 4 statements, 4 concerns: parsing csv, mapping fields, updating database, , efficiency/consistency (using prepared statement).

oop still relies on functional programming - organize functionality. above can remain script, called cron job or web page. can put in function 'import_csv' re-used these tasks. put in class overkill. soc makes use of facades or interfaces. function that, , grouped class if need be. you'd need function import_csv( $filename, $profile ). $profile indicate table (or tables), column mapping etc. separating out can make generic csv db mapping editor - or @ least support importing csv file table changing configuration. if make classes of you'll end writing 1 class every csv file you're going have import. can of course make csvimporter class, useful if there other importer classes - after all, make class abstract functionality, , cannot abstract 1 example.

imagine instead generic importer, regardless of data. focus on functionality, code is. far better approach, code not depend on database structure - you've abstracted (by extending table base class public interface can $table->save()). you'd not abstract csv (it not worthwhile, first line of code shows), mapping.

this not answer expected, i'm sure didn't want answer 1 gave, since posed question.


Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -