热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在PHP中使用'func_get_arg'的好例子是什么?-Whatisthegoodexampleofusing'func_get_arg'inPHP?

Ijusthavefoundoutthatthereisafunctioncalledfunc_get_arginPHPwhichenablesdeveloperto

I just have found out that there is a function called func_get_arg in PHP which enables developer to use variant style of getting arguments. It seems to be very useful because number of argument can now be arbitrary, but I cannot think of any good example of using it.

我刚刚发现PHP中有一个名为func_get_arg的函数,它允许开发人员使用获取参数的变体样式。它似乎非常有用,因为参数的数量现在可以是任意的,但是我想不出任何使用它的好例子。

What are the some examples of using this function to fully benefit its polymorphic characteristic?

用这个函数来充分利用它的多态特性的例子有哪些?

7 个解决方案

#1


22  

I usually use func_get_args() which is easier to use if wanting multiple arguments.

我通常使用func_get_args(),如果需要多个参数,则更容易使用。

For example, to recreate PHP's max().

例如,重新创建PHP的max()。

function max() {
   $max = -PHP_INT_MAX;
   foreach(func_get_args() as $arg) {
      if ($arg > $max) {
          $max = $arg;
      }
   }
   return $max;
}

CodePad.

CodePad。

Now you can do echo max(1,5,7,3) and get 7.

现在你可以做echo max(1,5,7,3)得到7。

#2


2  

First of all, you are using the term "polymorphism" totally wrong. Polymorphism is a concept in object-oriented programming, and it has nothing to do with variable number of arguments in functions.

首先,您使用的术语“多态性”是完全错误的。多态性是面向对象编程中的一个概念,它与函数中的变量数量无关。

In my experience, all func_get_args allows you to do is add a little syntactic sugar.

在我的经验中,所有func_get_args允许您添加一点语法糖。

Think of a function that can take any number of integers and return their sum. (I 'm cheating, as this already exists in array_sum. But cheating is good if it keeps the example simple). You could do it this way:

考虑一个函数,它可以取任意数量的整数并返回它们的和。(我在作弊,因为这在array_sum中已经存在。但是作弊如果能保持简单的例子就很好)。你可以这样做:

// you can leave "array" out; I have it because we should be getting one here
function sum1(array $integers) {
    return array_sum($integers);
}

Now you would call this like so:

你可以这样称呼它:

$sum = sum1(array(1));
$sum = sum1(array(1, 2, 3, 4));

This isn't very pretty. But we can do better:

这不是很漂亮。但我们可以做得更好:

function sum2() {
    $integers = func_get_args();
    return array_sum($integers);
}

Now you can call it like this:

现在你可以这样称呼它:

$sum = sum2(1);
$sum = sum2(1, 2, 3, 4);

#3


2  

Let's say we have multiple arrays containing data in which we need to search across the keys for their values without merging these arrays.

假设我们有多个数组,其中包含数据,我们需要跨键搜索它们的值,而不需要合并这些数组。

The arrays are like:

数组是:

$a = array('a' => 5, 'b' => 6);
$b = array('a' => 2, 'b' => 8);
$c = array('a' => 7, 'b' => 3);

In that case, say we need to get all the values of the key a from all the arrays. We can write a function that take in arbitrary number of arrays to search in.

在这种情况下,假设我们需要从所有数组中获取键a的所有值。我们可以编写一个函数,它接受任意数量的数组进行搜索。

// we need the key, and at least 1 array to search in
function simpleSearchArrays($key, $a1){
    $arrays = func_get_args();
    array_shift($arrays); // remove the first argument, which is the key
    $ret = array();
    foreach($arrays as $a){
        if(array_key_exists($key, $a)){
            $ret[] = $a[$key];
        }
    }
    return $ret;
}

So if we use the function:

如果我们使用这个函数

 $x = simpleSearchArrays('a', $a, $b, $c);

$x will then contain array(5, 2, 7).

$x将包含数组(5,2,7)。

#4


1  

Personally, I don't think there is a good use case for it inside a normal function. As a control freak I like to know exactly what is being passed to my functions and I like to know exactly what I'm passing.

就我个人而言,我不认为在一个普通的函数中有一个很好的用例。作为一个控制狂,我喜欢确切地知道传递给我的函数的是什么,我喜欢确切地知道我传递的是什么。

However, it can be use full for things like Dynamic/Static URL routing. When you are rewriting (via mod_rewrite) the URL args to a single bootstrap.

但是,它可以完全用于动态/静态URL路由。当您重写(通过mod_rewrite) URL时,将args重写为一个引导程序。

In this sense, you can have arguments that don't necessarily need to exist with every page request.

在这个意义上,您可以拥有不必在每个页面请求中都存在的参数。

#5


1  

I hardly ever use func_get_arg(), but I do use its cousin func_get_args() quite a bit. Here's one example, a function along the lines of the echo statement that entity encodes all its arguments:

我几乎不使用func_get_arg(),但我确实经常使用它的表亲func_get_args()。这里有一个例子,一个沿着echo语句的函数,实体会编码所有的参数:

function ee() {
    $args = func_get_args();
    echo implode('', array_map('htmlentities', $args));
}

I use that function quite a bit.

我经常用这个函数。

Here's another useful example, a function that does the same job as SQL's COALESCE() function.

下面是另一个有用的示例,该函数的工作与SQL的COALESCE()函数相同。

function coalesce() {
    $args = func_get_args();
    foreach ($args as $arg) {
        if (!is_null($arg)) {
            return $arg;
        }
    }
    return null;
}

It returns the first non-null argument passed in, or null if there's no such argument.

它返回传入的第一个非空参数,如果没有此类参数,则返回null。

#6


0  

Read the official document and found that func_get_args():
Gets an array of the function's argument list.

阅读官方文档,发现func_get_args():获取函数的参数列表的数组。

This function may be used in conjunction with func_get_arg() and func_num_args() to allow user-defined functions to accept variable-length argument lists.

这个函数可以与func_get_arg()和func_num_args()一起使用,以允许用户定义的函数接受可变长度参数列表。

= 2) {
                echo "Second argument is: " . func_get_arg(1) . "\n";
            }
            $arg_list = func_get_args();
            for ($i = 0; $i <$numargs; $i++) {
                echo "Argument $i is: " . $arg_list[$i] . "\n";
            }
        }

        foo(1, 2, 3);
?>

#7


0  

As of php5.6, there isn't a use case of func_get_arg anymore; which isn't to say that its functionality isn't needed anymore, but is replaced by the variadic function using the ... syntax:

从php5.6开始,就不再有func_get_arg的用例了;这并不是说它的功能不再需要了,而是用…语法:

/**
 * @param array $arguments
 */
public function poit(...$arguments)
{
     foreach($arguments as $argument) {
         ...
     }
}

This is especially useful if there are methods that are overloaded at the end; one does need to filter out the first arguments anymore, as showcased by an example:

如果有在最后重载的方法,这一点尤其有用;一个人需要过滤掉第一个参数,就像一个例子展示的那样:

Old style using func_get_arg:

使用func_get_arg旧风格:

function foo($a, $b) {
    $c = array();
    if (func_num_args() > 2) {
        for($i = 0; $i 

Newer variadic style;

新的可变风格;

function foo($a, $b, …$c) {
    var_dump($a, $b, $c);
    // Do something
}
foo('a', 'b', 'c', 'd');

Both foo produce the same output, one is much simpler to write.

两个foo生成相同的输出,一个要简单得多。


推荐阅读
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文介绍了一种解析GRE报文长度的方法,通过分析GRE报文头中的标志位来计算报文长度。具体实现步骤包括获取GRE报文头指针、提取标志位、计算报文长度等。该方法可以帮助用户准确地获取GRE报文的长度信息。 ... [详细]
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
  • 本文讨论了编写可保护的代码的重要性,包括提高代码的可读性、可调试性和直观性。同时介绍了优化代码的方法,如代码格式化、解释函数和提炼函数等。还提到了一些常见的坏代码味道,如不规范的命名、重复代码、过长的函数和参数列表等。最后,介绍了如何处理数据泥团和进行函数重构,以提高代码质量和可维护性。 ... [详细]
  • Java SE从入门到放弃(三)的逻辑运算符详解
    本文详细介绍了Java SE中的逻辑运算符,包括逻辑运算符的操作和运算结果,以及与运算符的不同之处。通过代码演示,展示了逻辑运算符的使用方法和注意事项。文章以Java SE从入门到放弃(三)为背景,对逻辑运算符进行了深入的解析。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • PHP中的单例模式与静态变量的区别及使用方法
    本文介绍了PHP中的单例模式与静态变量的区别及使用方法。在PHP中,静态变量的存活周期仅仅是每次PHP的会话周期,与Java、C++不同。静态变量在PHP中的作用域仅限于当前文件内,在函数或类中可以传递变量。本文还通过示例代码解释了静态变量在函数和类中的使用方法,并说明了静态变量的生命周期与结构体的生命周期相关联。同时,本文还介绍了静态变量在类中的使用方法,并通过示例代码展示了如何在类中使用静态变量。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 摘要: 在测试数据中,生成中文姓名是一个常见的需求。本文介绍了使用C#编写的随机生成中文姓名的方法,并分享了相关代码。作者欢迎读者提出意见和建议。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有