詳解WordPress中用于合成數(shù)組的wp_parse_args()函數(shù)
wp_parse_args() 函數(shù)是 WordPress 核心經(jīng)常用到的函數(shù),它的用途很多,但最主要用來給一個數(shù)組參數(shù)(args)綁定默認(rèn)值。
因為 wp_parse_args() 函數(shù)返回的一定是一個數(shù)組,所以他會把傳入查詢字符串和對象(object)自動轉(zhuǎn)換成數(shù)組,給了使用者更加方便的條件,也增加了兼容性。
常見的 query_posts()、wp_list_comments() 和 get_terms() 函數(shù)都使用了 wp_parse_args() 函數(shù)來幫它給數(shù)組參數(shù)添加默認(rèn)值。
用法
wp_parse_args( $args, $defaults );
參數(shù)
$args
(數(shù)組 | 字符串)(必須)查詢字符串、對象或者數(shù)組參數(shù),用來綁定默認(rèn)值。
默認(rèn)值:None
查詢字符串:
type=post&posts_per_page=5&cat=1
數(shù)組:
array( 'type' => 'post', 'posts_per_page' => 5, 'cat' => '1' )
$defaults
(數(shù)組)(可選)數(shù)組參數(shù)的默認(rèn)參數(shù)。
默認(rèn)值:空字符串
例子
function explain_parse_args( $args = array() ){
//$args 的默認(rèn)值
$defaults = array(
'before' => '<div class="box">',
'after' => '</div>',
'echo' => true,
'text' => 'wp_parse_args() 函數(shù)演示'
);
//綁定默認(rèn)值
$r = wp_parse_args( $args, $defaults );
$output = $r['before'] . $r['text'] . $r['after'];
if( !$r['echo'] ) return $output;
echo $output;
}
//沒有參數(shù)
explain_parse_args();//打?。?lt;div class="box">wp_parse_args() 函數(shù)演示</div>
//字符串參數(shù)
$output = explain_parse_args( 'text=字符串參數(shù)&before=<div class="box-2">&echo=0' );
echo $output;//打印:<div class="box-2">字符串參數(shù)</div>
//數(shù)組參數(shù)
explain_parse_args( array( 'text' => '數(shù)組參數(shù)', 'before' => '<div class="box-3">' ) );//打?。?lt;div class="box-3">數(shù)組參數(shù)</div>
還有另一種不使用第二個 $defaults 參數(shù)的用法,就是幫你把一個查詢字符串、對象或者數(shù)組的變量直接轉(zhuǎn)換成通用的數(shù)組,避免判斷類型。
//字符串
$array = wp_parse_args( 'text=測試另一種用法&type=字符串' );
var_dump( $array );
/*
array(2) {
["text"]=>
string(21) "測試另一種用法"
["type"]=>
string(9) "字符串"
}
*/
//對象(object)
class args_obj{
public $text = '測試另一種用法';
public $type = '對象(object)';
function func(){
//轉(zhuǎn)換成數(shù)組的時候?qū)ο罄镞叺暮瘮?shù)會被忽略
}
}
$obj = new args_obj;
var_dump( $obj );
/*
object(args_obj)#2175 (2) {
["text"]=>
string(21) "測試另一種用法"
["type"]=>
string(18) "對象(object)"
}
*/
wp_parse_args函數(shù)源代碼詳解
wp_parse_args 函數(shù)的源代碼比較簡單,
依附于PHP 內(nèi)置函數(shù)get_object_vars、array_merge與WordPress的wp_parse_str函數(shù)來實現(xiàn),
以下是該函數(shù)的源代碼:
/**
* Merge user defined arguments into defaults array.
*
* This function is used throughout WordPress to allow for both string or array
* to be merged into another array.
*
* @since 2.2.0
*
*第一個參數(shù)可以是 字符串、數(shù)組或?qū)ο螅╫bj)
* @param string|array $args Value to merge with $defaults
*第二個參數(shù)為默認(rèn)的預(yù)設(shè)值數(shù)組,必須是數(shù)組
* @param array $defaults Array that serves as the defaults.
*返回值將是一個數(shù)組
* @return array Merged user defined values with defaults.
*/
function wp_parse_args( $args, $defaults = '' ) {
if ( is_object( $args ) )
//將接收的對象(obj)轉(zhuǎn)換為數(shù)組
$r = get_object_vars( $args );
elseif ( is_array( $args ) )
//如果是數(shù)組則不轉(zhuǎn)換
$r =& $args;
else
//將接收的字符串轉(zhuǎn)換為數(shù)組
wp_parse_str( $args, $r );
if ( is_array( $defaults ) )
return array_merge( $defaults, $r );
return $r;
}
其中g(shù)et_object_vars函數(shù)是用來返回由對象屬性組成的關(guān)聯(lián)數(shù)組。
array_merge函數(shù)用是將兩個或多個數(shù)組的單元合并起來,一個數(shù)組中的值附加在前一個數(shù)組的后面。返回作為結(jié)果的數(shù)組。
相關(guān)文章
將酷狗krc歌詞解析并轉(zhuǎn)換為lrc歌詞php源碼
這篇文章主要介紹了krc歌詞解析并轉(zhuǎn)換為lrc歌詞的php實現(xiàn)方法,需要的朋友可以參考下2014-06-06
php面向?qū)ο缶幊蘳elf和static的區(qū)別
這篇文章主要介紹了PHP中static關(guān)鍵字以及與self關(guān)鍵字的區(qū)別,本文講解了static關(guān)鍵字的定義、遲綁定(Late Static Bindings)、以及與self關(guān)鍵字的區(qū)別等內(nèi)容,需要的朋友可以參考下2016-05-05
PHP中file_exists與is_file,is_dir的區(qū)別介紹
很顯然file_exists是受了asp的影響,因為asp不但有fileExists還有folderExists,driverExists,那么PHP中file_exists是什么意思呢2012-09-09

