首頁  >  文章  >  後端開發  >  PHP 原始碼 — is_array 函數原始碼分析

PHP 原始碼 — is_array 函數原始碼分析

步履不停
步履不停原創
2019-06-25 15:34:562566瀏覽

PHP 原始碼 — is_array 函數原始碼分析

php 中的is_array

php 中的is_array,它的簽章是is_array ( mixed $var ) : bool

實作的原始碼

\ext\standard\type.c 中可以找到 PHP_FUNCTION(is_array) 所處的位置,大概位於273 行。

在PHP 中,這個系列的函數,是由很多個,除了它本身之外,還有is_bool 、 is_countable 、 is_callback 、 is_int 、 is_object 、 is_string 等等

#在它們之中,大部分的原始碼也都是和is_array 的類似:

PHP_FUNCTION(is_array)
{
php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_ARRAY);
}

它的定義很簡潔,直接呼叫了 php_is_type ,巨集 INTERNAL_FUNCTION_PARAM_PASSTHRU 的作用是,將呼叫is_array 時的參數,原樣傳遞給php_is_type 。它的定義如下:

#define INTERNAL_FUNCTION_PARAM_PASSTHRU execute_data, return_value

函數php_is_type 的定義如下:

static inline void php_is_type(INTERNAL_FUNCTION_PARAMETERS, int type)
{
zval *arg;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ZVAL(arg)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
if (Z_TYPE_P(arg) == type) {
if (type == IS_RESOURCE) {
const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg));
if (!type_name) {
RETURN_FALSE;
}
}
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}

前面幾行是參數解析部分

ZEND_PARSE_PARAMETERS_START(1, 1)
    Z_PARAM_ZVAL(arg)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

接著透過 Z_TYPE_P(arg) 取得變數的類型,再讓其結果和 IS_ARRAY 判等。如果為真,則表示變數是數組,否則不是。 Z_TYPE_P 的作用很明顯,就是取得變數的類型,這個巨集展開後如下:

static zend_always_inline zend_uchar zval_get_type(const zval* pz) {
return pz->u1.v.type;
}

其中的pz ,就是zval 指針, zval 就是常提到的 _zval_struct:

struct _zval_struct {
zend_value        value;/* 值 */
union {
struct {
ZEND_ENDIAN_LOHI_3(
zend_uchar    type,/* 类型 */
zend_uchar    type_flags,
union {
uint16_t  call_info;    /* call info for EX(This) */
uint16_t  extra;        /* not further specified */
} u)
} v;
uint32_t type_info;
} u1;
union {
uint32_t     next;                 /* hash 碰撞时用到的链表 */
uint32_t     cache_slot;           /* cache slot (for RECV_INIT) */
uint32_t     opline_num;           /* opline number (for FAST_CALL) */
uint32_t     lineno;               /* 行号 (ast 节点中) */
uint32_t     num_args;             /* 参数数量 for EX(This) */
uint32_t     fe_pos;               /* foreach 时的所在位置 */
uint32_t     fe_iter_idx;          /* foreach iterator index */
uint32_t     access_flags;         /* 类时的访问权限标志位 */
uint32_t     property_guard;       /* single property guard */
uint32_t     constant_flags;       /* constant flags */
uint32_t     extra;                /* 保留字段 */
} u2;
};

不做深入介紹了。

接續看 php_is_type在判斷型別時,

有個地方比較蹊蹺: if (type == IS_RESOURCE) {

#為何這裡要判斷是否是資源類型?

延伸資源型別

這裡延伸一下,如果用php_is_type 判斷的是資源型別

這裡會呼叫 const char *type_name = zend_rsrc_list_get_rsrc_type(Z_RES_P(arg));

其中有zend_rsrc_list_get_rsrc_type 的調用,其實作如下:

const char *zend_rsrc_list_get_rsrc_type(zend_resource *res)
{
zend_rsrc_list_dtors_entry *lde;
lde = zend_hash_index_find_ptr(&list_destructors, res->type);
if (lde) {
return lde->type_name;
} else {
return NULL;
}
}

有一個叫做 list_destructors

##的靜態變數的靜態變數,它的靜態變數,它的靜態變數,它的靜態變數,作用如下

list_destructors 是一個全域靜態HashTable,資源型別註冊時,將一個zval 結構變數zv 存放在list_destructors 的arData 中,而zv 的value.ptr 卻指向了zend_rsrc_list_dtors_entry *lde , lde中所包含的該種資源釋放函數指標、持久資源的釋放函數指針,資源型別名稱,該資源在hashtable 中的索引依據(resource_id)等。 --源自於「PHP7 使用資源包第三方擴充原理分析」

#也就是說,當創建了一個資源型別R1時,就會向list_destructors# 存入一份zend_rsrc_list_dtors_entry

,其中包含了該資源R1的一些資訊

這裡的 zend_hash_index_find_ptr 就是找到資源對應的 zend_rsrc_list_dtors_entry

lde->type_name

如果type 成員是存在的,則表示是資源類型。

總結

PHP 中使用 is_* 系列判斷類型的函數,大部分都是透過變數底層zval 中的

u1 .v.type

來判斷類型值如果是資源類型,需要透過list_destructors 查詢對應的資源類型是否存在,如果存在,說明資源句柄是可以正常使用的。 更多PHP相關技術文章,請造訪

PHP教學###欄位進行學習! ###

以上是PHP 原始碼 — is_array 函數原始碼分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn