coffeemonitor
coffeemonitor

Reputation: 13120

PHP Class - Retrieve Method's Data

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.

Upvotes: 0

Views: 74

Answers (3)

AlexP
AlexP

Reputation: 9857

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
);

Upvotes: 0

mehmetseckin
mehmetseckin

Reputation: 3107

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');
// ...

Upvotes: 3

Phil Cross
Phil Cross

Reputation: 9302

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 />';
}

Upvotes: 2

Related Questions