-1

我有一个这样的 PHP 函数:

function count_nutrient_value($input){
 // some difficult operations here
 return $output; // this is a string
}

我正在使用这样的(示例):

$print .= 'This nutrient is giving you '.count_nutrient_value(40).' percent.';

现在的问题是,从函数中,我需要返回另一个值 - 一个数字是上升还是下降的信息。所以我改变了功能:

function count_nutrient_value_new($input){
 // some difficult operations here
 return array($output,$status); // this is an array, obviously
}

但是现在我不能那么容易地使用该功能,因为我不能再这样做了:

$print .= 'This nutrient is giving you '.count_nutrient_value_new(40).' percent.';

但相反,我需要将其扩展到更多行,如下所示:

$return = count_nutrient_value_new(40);
$print .= 'This nutrient is giving you '.$return[0].' percent.';

这种情况有解决办法吗?这将是:

A)不从函数返回数组(但以其他方式解决问题)
B)返回数组并在代码中找到使用该函数的更简单方法
C)其他?

4

6 回答 6

4

您为每个目的创建两个函数,并从另一个调用一个。

function count_nutrient_value_string( $input ){
    $array = count_nutrient_value_array( $input );
    return $array[ 0 ];
}

function count_nutrient_value_array( $input ){
     // some difficult operations here
     return array( $output, $status ); 
}

$print .= 'This nutrient is giving you '.count_nutrient_value_string(40).' percent.';

(函数名称应该比它们实际做的更好,_string_array取决于它们的实际作用。)

于 2013-06-20T06:54:57.453 回答
2

我会避免更改函数的签名,因为它可能会破坏某些代码。相反,我宁愿添加并调用一个新函数。

无论如何,如果我必须按照您在此处描述的方式更改函数,我会使用实用程序类而不是数组。

  • 该类将有两个属性:$count 和 $status,你是否将它们公开是方便的问题。在你的情况下,我想公众的态度很好。
  • 该类还可以实现 __toString() 方法以保持与旧代码的兼容性。

就像是 :

class nutrientResults {

    public $status = SOME_DEFAULT_VALUE;
    public $count = 0;

    public function __construct($count, $st = SOME_DEFAULT_VALUE) {
         $this->count = $count;
         $this->status = $st;
    }

    public function __toString() {
        return (string) $this->count;
    }

}

然后 :

function count_nutrient_value($input){
    // some difficult operations here
    return new nutrientResults($count, $status);
}

$print .= 'This nutrient is giving you '.count_nutrient_value(40).' percent.';
// as usual..., so backwards compatibility maintained
于 2013-06-20T06:58:58.807 回答
0

您可以使用输出参数。

function count_nutrient_value($input, &$out1, &$out2){
 // some difficult operations here
 $out1 = $output[0];
 $out2 = $output[1];
 return $output[0]; // return the same
}

$print .= 'This nutrient is giving you '.count_nutrient_value(40, $o1, $o2).' percent.';
// Do other things with $o1 and $o2.

在 php 5 中,您还可以使用可选参数:

function count_nutrient_value($input, &$out1 = '', &$out2 = ''){
 // some difficult operations here
 $out1 = $output[0];
 $out2 = $output[1];
 return $output[0]; // return the same
}

// Call without out parameters
$print .= 'This nutrient is giving you '.count_nutrient_value(40).' percent.';
于 2013-06-20T07:01:20.940 回答
0

将函数变成一个小类,$output并以 $status 作为属性:

class count_nutrient_value() {
public $output;
public $status;
function __construct($input) {
   // Do complicate stuff here
   $this->output = $output_result;
   $this->status = $status_result;
}
}

然后你可以做

$nutrient = new count_nutrient_value($input);
$print .= 'This nutrient is giving you '.$nutrient->output.' percent.';
于 2013-06-20T07:01:42.267 回答
0

拆分数据源和一些访问器/转换器中的功能可能是正确的选择。
如果您正在使用数据源返回的数组/对象的所有(或大部分)信息,那么存储/缓存它一次而不是每次调用访问器函数时都重新创建可能会更好。
作为您现在所拥有的简单替代品,请查看v printf 函数系列。他们没有使用省略号(printf($format, ...)例如printf('%d %s %d', 1,'a', 3)),而是采用一组参数:vprintf('%d %s %d', array(1,'a', 3))可以轻松地从另一个函数传递或传递给另一个函数。

<?php
function count_nutrient_value($input) {
    return array($input*0.1, ' approved');
}

$result = vsprintf('This%2$s nutrient is giving you %1$d percent.', count_nutrient_value(40));
echo $result;

http://docs.php.net/function.vsprintf

于 2013-06-20T07:05:32.237 回答
-2

您可以使用示例代码返回数组值:

function count_nutrient_value_new($input){
 // some difficult operations here
 return array('output' => $output, 'status' => $status); // this is an array, obviously
}

然后得到的价值是:

$print .= 'This nutrient is giving you '.$return["output"].' percent.';
于 2013-06-20T06:57:20.413 回答