2011-08-30 87 views
0

我想弄清楚如何使用算法的输出作为findLarge的输入。 算法产生,我想在findLarge上工作的阵列如何将一种方法的输出用作另一种方法的输入?

class CSVParser 
{ 

    public $output = NULL; 
    public $digits = NULL; 
    public $largest = NULL; 

    public function __construct($file) 
    { 


     if (!file_exists($file)) { 
      throw new Exception("$file does not exist"); 
     } 

     $this->contents = file_get_contents($file); 
     $this->output = array(); 
     $this->digits = array(); 
     $this->largest = array(); 
    } 

    public function algorithm() {....} 

    public function findLarge($a) 
    { 
     // just push it back so I know it's working 
     var_export($a); // is NULL 
     $this->largest = $a; // return NULL 
    } 

} 

$parser->algorithm(); 
$parser->findlarge($input); print_r($parser->largest); 
+2

为什么不能'$ parser-> findlarge($ parser-> algorithm());'? –

回答

1

它看起来像你只是在寻找:

$parser->findlarge($parser->algorithm()); 

你可能想不过考虑,几件事情。

  1. PHP已经有str_getcsv
  2. 无论它可能是一个更好的主意为algorithm调用自身findLarge。看起来这个班有两个相互冲突的目的。一个是存储数据,另一个是处理数据。您可能想要考虑具有无状态的algorithm并具有单独的DO,或者让算法修改实例的状态。
相关问题