简体   繁体   English

在函数调用中使用“命名”参数

[英]Using “named” parameters in function call

Unfortunately PHP does not support named parameters, but I've noticed that the PHP syntax allows us something similar: 不幸的是,PHP不支持命名参数,但我注意到PHP语法允许我们进行以下操作:

function list_items( $user, $archived ) { ... }

// This is the default way to call the function.
// It's not very clear what the value and false refer to:
list_items( 10, false );

// Alternative syntax I found which makes this more clear:
list_items( $user = 10, $archived = false );

Both function calls return the same data. 两个函数调用都返回相同的数据。

Question

  • Any information on the actual difference between both calls? 有关两次通话之间实际差异的任何信息?
  • Is it a good idea to use the second kind of syntax or will it cause issues? 使用第二种语法是个好主意,还是会引起问题?
  • Is that second syntax supported in any PHP 5.x version? 任何PHP 5.x版本都支持第二种语法吗?

To answer this question: There are no named parameters. 要回答这个问题:没有命名参数。

The example in my question simply assigns values to variables and then passes those variables to the function. 我问题中的示例只是将值分配给变量,然后将这些变量传递给函数。

list_items( $user = 10, $archived = false );

// is same as this:
$user = 10;
$archived = false;
list_items( $user, $archived );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM