我有一个这样的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) ?
您为每个目的创建两个函数,并从另一个调用一个。
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
好,这取决于它们实际做什么。)
我会避免更改函数的签名,因为它可能会破坏某些代码。相反,我宁愿添加并调用一个新函数。
无论如何,如果我必须以您在这里描述的方式更改函数,我会使用实用程序类而不是数组。
- 该类将有两个属性:$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
将函数变成一个小类,以$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.';
在数据源和一些访问器/转换器中拆分函数可能是正确的选择。
如果你使用所有(或大部分)的数组/对象的信息返回你的数据源,它仍然可能更好的存储/缓存它一次,而不是重新创建每次访问器函数被调用。
作为一个简单的替换,我们来看一下vprintf函数族。它们不使用省略号(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
可以使用输出参数
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.';
您可以使用示例代码返回数组值:
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.';