【问题标题】:How to use the output of one method as the input on another?如何使用一种方法的输出作为另一种方法的输入?
【发布时间】:2011-08-30 22:12:59
【问题描述】:

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

【问题讨论】:

  • 为什么不$parser->findlarge($parser->algorithm());

标签: php arrays oop methods


【解决方案1】:

看起来你只是在寻找:

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

不过,您可能需要考虑几件事。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 2012-03-10
    相关资源
    最近更新 更多