oop - Possible improvements for an OO PHP written script -


okay, wanting expand armoury within php, i've been researching oo php. researched knowledge went on create quick script reads csv file , outputs results.

csv class:

class csv {      private $file;       public function __construct($filename, $mode) {         $this->file = fopen($filename, $mode);      }      public function endfile() {         return feof($this->file);      }      public function getcsv($mode) {         return fgetcsv($this->file, $mode);     }       public function close() {         fclose($this->file);      } } 

the test file:

require('class.csv.php');  $csv = new csv('postcodes.csv', 'r');   while(!$csv->endfile()) {      $postcode = $csv->getcsv(1024);      echo $postcode[0] . "<br />";   }  $csv->close(); 

i wondering if there are... or improvements make in regards oo approach. purely script me put knowledge i've learnt together. i'm not 'following crowd person' creates every script in oo approach because 'can'.

i understand short script wanting make sure have correct approach before moving forward.

the possibly thing can think of improve code have less in main test file. general idea of using object-oriented code can lot class , less in main file.

so example, needed you're doing in test file again later on in code. need complete duplicate while loop again. instead, maybe write method handles algorithm while loop doing , call instead. later on, call method again , it's simple!

the best part is, can use methods class inside other methods in class. endfile() method called method saying $this->endfile() same way calling $file saying $this->file

otherwise, i'd you're looking good!


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -