为什么PHP compact()使用字符串而不是实际变量?

时间:2022-02-22 11:49:23

Can anyone explain the benefit of PHP's compact() function accepting the string of 'a variable with that name' instead of the actual variable?

任何人都可以解释PHP的compact()函数接受“具有该名称的变量”而不是实际变量的字符串的好处吗?

For example:

例如:

$foo = 'foo';
$bar = 'bar';

$compacted = compact('foo', 'bar');

Why do I need to pass a string of the variable name instead of just passing the variable itself and PHP handling mapping this to an array? Like so:

为什么我需要传递变量名的字符串而不是仅传递变量本身和PHP处理将其映射到数组?像这样:

$compacted = compact($foo, $bar);

3 个解决方案

#1


19  

Because the compact() function needs to know the names of the variables since it is going to be using them as array keys. If you passed the variables directly then compact() would not know their names, and would not have any value to use for the returned array keys.

因为compact()函数需要知道变量的名称,因为它将把它们用作数组键。如果直接传递变量,那么compact()将不知道它们的名称,并且没有任何值可用于返回的数组键。

However, I suggest building the array manually:

但是,我建议手动构建阵列:

$arr = array(
    'foo' => $foo,
    'bar' => $bar);

I consider compact() deprecated and would avoid using it in any new code.

我认为compact()已弃用,并且会避免在任何新代码中使用它。

#2


30  

As far as benefits, I've found compact() to be useful in MVC applications. If you're writing controller code and you need to pass an associative array of variables and their apparent names that you've set in your controller to the view, it shortens something like:

至于好处,我发现compact()在MVC应用程序中很有用。如果您正在编写控制器代码,并且需要将您在控制器中设置的变量及其明显名称的关联数组传递给视图,则会缩短以下内容:

View::make('home')->with(array('products' => $products, 'title' => $title, 'filter' => $filter'));

to

View::make('home')->with(compact('products', 'title', 'filter'));

#3


8  

compact is a function and not a language construct. There is no way for PHP functions to know the names of the variables passed to them. In theory, compact could be implemented as a language construct like unset or isset and work the way you described. But that's not what happened.

compact是一个函数,而不是一个语言结构。 PHP函数无法知道传递给它们的变量的名称。从理论上讲,compact可以实现为unset或isset等语言结构,并按照你描述的方式工作。但事情并非如此。

#1


19  

Because the compact() function needs to know the names of the variables since it is going to be using them as array keys. If you passed the variables directly then compact() would not know their names, and would not have any value to use for the returned array keys.

因为compact()函数需要知道变量的名称,因为它将把它们用作数组键。如果直接传递变量,那么compact()将不知道它们的名称,并且没有任何值可用于返回的数组键。

However, I suggest building the array manually:

但是,我建议手动构建阵列:

$arr = array(
    'foo' => $foo,
    'bar' => $bar);

I consider compact() deprecated and would avoid using it in any new code.

我认为compact()已弃用,并且会避免在任何新代码中使用它。

#2


30  

As far as benefits, I've found compact() to be useful in MVC applications. If you're writing controller code and you need to pass an associative array of variables and their apparent names that you've set in your controller to the view, it shortens something like:

至于好处,我发现compact()在MVC应用程序中很有用。如果您正在编写控制器代码,并且需要将您在控制器中设置的变量及其明显名称的关联数组传递给视图,则会缩短以下内容:

View::make('home')->with(array('products' => $products, 'title' => $title, 'filter' => $filter'));

to

View::make('home')->with(compact('products', 'title', 'filter'));

#3


8  

compact is a function and not a language construct. There is no way for PHP functions to know the names of the variables passed to them. In theory, compact could be implemented as a language construct like unset or isset and work the way you described. But that's not what happened.

compact是一个函数,而不是一个语言结构。 PHP函数无法知道传递给它们的变量的名称。从理论上讲,compact可以实现为unset或isset等语言结构,并按照你描述的方式工作。但事情并非如此。