0

I have a simple Class for telling the statistics of a CSV file.

One of the methods is for breaking down certain parts of the csv file I specify in the constructor.

I'm not sure how to get those variables selectively, so I just echo them all out. But I want to place them into their own variables for me to grab, after I instantiate the object.

class.OpenCSV.php

<?php
class OpenCSV {
   private $filepath; 

   public function __construct($filepath = __FILE__){
     $this->filepath = $filepath;
     if(!is_readable($filepath)){
     echo("Filepath not found or not readable"."\n");
     exit();
   }

   public function reviewFileStats(){
      echo "-----------------------------------\n";
      $path_parts = pathinfo($this->filepath); 
      echo "filepath: \t".$this->filepath. "\n"; 
      echo "dirname: \t".$path_parts['dirname']. "\n"; 
      echo "basename: \t".$path_parts['basename']. "\n"; 
      echo "filename: \t".$path_parts['filename']. "\n";  
      echo "extension: \t".$path_parts['extension']. "\n"; 
      echo "-----------------------------------\n";
   }//eof reviewFileStats

}
?>

showfile.php

<?php
require_once('class.OpenCSV.php');

$csvfile = 'some_csv_file.csv';
$csvObj = new OpenCSV($csvfile);
$csvObj->reviewFileStats();

?>

For now, it displays:

-----------------------------------
filepath:       /home/charlie/documents/some_csv_file.csv
dirname:        /home/charlie/documents
basename:       some_csv_file.csv
filename:       some_csv_file
extension:      csv
-----------------------------------

And that's great, until I just want to select only one of those items. How do I just retrieve each one separately when calling the method reviewFileStats

I know this is an easy one, but I'm still new to the OO world.

3 Answers 3

3

You could use a specific get method like this :

public function getFileStat($stat = null) {
    if($stat) {
       $path_parts = pathinfo($this->filepath); 

       // Return the info if it exists, null if not.
       return isset($path_parts[$stat]) ? $path_parts[$stat] : null;
    }
    // Return the filepath if $stat is not specified.
    return $this->filepath;
}

And you can use it like this :

$csvfile = 'some_csv_file.csv';
$csvObj = new OpenCSV($csvfile);
$filepath = $csvObj->getFileStat();
$basename = $csvObj->getFileStat('basename');
// ...
Sign up to request clarification or add additional context in comments.

Comments

2

Change your reviewFileStatistics function to this:

public function reviewFileStats($return_what = 'all'){

  $path_parts = pathinfo($this->filepath);

  $filepath = $this->filepath;
  $dirname = $path_parts['dirname'];
  $basename = $path_parts['basename'];
  $filename = $path_parts['filename'];
  $extension = $path_parts['extension'];

  if($return_what==='all')
  {
      return array('filepath' => $filepath, 'dirname' => $dirname, 'basename' => $basename, 'filename' => $filename, 'extension' => $extension);
  }

  if(isset($$return_what))
  {
      return $$return_what;
  }

  return false;
} 

Then you can call items like this:

echo 'Filepath: ' . $csvObj->reviewFileStats('filepath');  // Outputs the filepath
echo 'Filename: ' . $csvObj->reviewFileStats('filename'); // Outputs the filenmae

If you want to output everything:

foreach($csvObj->reviewFileStats('all') as $key => $value)
{
   echo $key . ': ' . $value . '<br />';
}

4 Comments

Erm, whats the downvote for? This works fine. And is what the user asked for.
Indeed, what was the downvote for? I've just upvoted this, as it's similar but a better version of my answer to this question.
Thank you @Seçkin , I'll upvote yours as it is a more elegant solution.
Both of you guys rock. Thank you for your examples. I'm upvoting both of you.
0

It might make more sence to encapsulate the file infomation into another class, say a FileInfo class.

class FileInfo
{
  public $dirname;

  public $basename;

  public $filename;

  public $extension;

  public function __construct($filePath)
  {
    $this->init($filePath);
  }

  public function init($filePath)
  {
    $data = pathinfo($this->filepath); 
    $this->dirname   = $data['dirname'];
    $this->basename  = $data['basename'];
    $this->filename  = $data['filename'];
    $this->extension = $data['extension'];
  }

}

You would be able to then attach the class to the OpenCsv.

class OpenCsv 
{
  protected $filePath;

  protected $fileInfo;

  public function __construct($filePath)
  {
    $this->setFilePath($filePath);
  }

  public function setFilePath($path)
  {
    $this->fileInfo = new FileInfo($path);
    $this->filePath = $path;
  }

  public function getFileInfo()
  {
    return $this->fileInfo;
  }
}

Then in client code

$csv  = new OpenCsv('/foo/bar/file.txt');
$info = $csv->getFileInfo();

echo sprintf(
  'The file name \'%s\' has the extention \'%s\'', 
  $info->filename,
  $info->extention
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.