簡體   English   中英

在函數調用中使用“命名”參數

[英]Using “named” parameters in function call

不幸的是,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 );

兩個函數調用都返回相同的數據。

  • 有關兩次通話之間實際差異的任何信息?
  • 使用第二種語法是個好主意,還是會引起問題?
  • 任何PHP 5.x版本都支持第二種語法嗎?

要回答這個問題:沒有命名參數。

我問題中的示例只是將值分配給變量,然后將這些變量傳遞給函數。

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