PHP类 - 检索方法的数据

时间:2014-02-10 01:04:07

标签: php class methods

我有一个简单的类来告诉CSV文件的统计信息。

其中一种方法是分解我在构造函数中指定的csv文件的某些部分。

我不确定如何有选择地获取这些变量,所以我只是回应它们。 但是在我实例化对象之后,我想将它们放入自己的变量中以供我使用。

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

?>

目前,它显示:

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

这很好,直到我只想选择其中一个项目。 如何在调用方法 reviewFileStats

时单独检索每个方法

我知道这很简单,但我仍然是OO世界的新手。

3 个答案:

答案 0 :(得分:3)

您可以使用这样的特定get方法:

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

你可以像这样使用它:

$csvfile = 'some_csv_file.csv';
$csvObj = new OpenCSV($csvfile);
$filepath = $csvObj->getFileStat();
$basename = $csvObj->getFileStat('basename');
// ...

答案 1 :(得分:2)

将reviewFileStatistics函数更改为:

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

然后你可以调用这样的项目:

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

如果您想输出所有内容:

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

答案 2 :(得分:0)

将文件信息封装到另一个类(例如FileInfo类中)可能会更加明确。

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

}

然后,您可以将课程附加到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;
  }
}

然后在客户端代码中

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

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