如何使用一个方法的输出作为另一个方法的输入?

时间:2011-08-30 22:13:00

标签: php arrays oop methods

我正在试图弄清楚如何使用算法的输出作为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);

1 个答案:

答案 0 :(得分:1)

看起来你只是在寻找:

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

然而,您可能需要考虑几件事情。

  1. PHP已有str_getcsv
  2. algorithm是否可以更好地自行调用findLarge。看起来这个类有两个相互矛盾的目的。一个是存储数据,另一个是处理数据。您可能想要考虑让algorithm无状态并具有单独的DO,或者让算法修改实例的状态。