php擴充開發筆記(5)一些陣列操作相關的巨集和簡單範例

WBOY
發布: 2016-08-08 09:22:47
原創
998 人瀏覽過

實現類似isset($array[$value]) 功能,php程式碼如下

<code><span><?php</span><span><span>class</span><span>Slash_Log</span> {</span><span>protected</span><span>static</span><span>$levels</span> = <span>array</span>(
        <span>1</span> => <span>"ERROR"</span>,
        <span>2</span> => <span>"WARNING"</span>,
        <span>3</span> => <span>"INFO"</span>,
        <span>4</span> => <span>"DEBUG"</span>,
    );

    <span>public</span><span><span>function</span><span>setLevel</span><span>(<span>$level</span>)</span> {</span><span>if</span> (!<span>isset</span>(<span>self</span>::<span>$levels</span>)) {
            <span>throw</span><span>new</span><span>Exception</span>(<span>"Level is not allowed"</span>);
        }
    }
}</span></code>
登入後複製

C 程式碼如下

<code>PHP_METHOD(slash_log, setLevel) {

    <span>long</span> level;
    zval *levels;
    zend_class_entry *ce;
    HashTable *levels_ht;

    ce = Z_OBJCE_P(getThis());
    <span>if</span> (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, <span>"l"</span>, &level) == FAILURE)
    {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, <span>"Invalid argument"</span>);
        RETURN_FALSE;
    }

    levels = zend_read_static_property(ce, ZEND_STRL(SL_P_LEVELS), <span>0</span> TSRMLS_DC);
    levels_ht = Z_ARRVAL_P(levels);

    <span>if</span> (!zend_hash_index_exists(levels_ht, level)) {
        php_error_docref(NULL TSRMLS_CC, E_WARNING, <span>"Level is not allowed"</span>);
        RETURN_FALSE;
    }

    zend_update_property_long(ce, getThis(), ZEND_STRL(SL_P_LEVEL), level TSRMLS_CC);
    RETURN_TRUE;
}</code>
登入後複製

上面的例子中,主要是使用zend_hash_index_exists 方法,關於操作有一系列的方法,關於操作有一系列操作的table 的操作有一系列操作,關於操作有一系列操作的table Zend/zend_hash.h

<code>ZEND_API <span>int</span> zend_hash_add_empty_element(HashTable *ht, <span>const</span><span>char</span> *arKey, uint nKeyLength);
ZEND_API <span>int</span> zend_hash_del_key_or_index(HashTable *ht, <span>const</span><span>char</span> *arKey, uint nKeyLength, ulong h, <span>int</span> flag);
ZEND_API <span>int</span> zend_hash_find(<span>const</span> HashTable *ht, <span>const</span><span>char</span> *arKey, uint nKeyLength, <span>void</span> **pData);
ZEND_API <span>int</span> zend_hash_quick_find(<span>const</span> HashTable *ht, <span>const</span><span>char</span> *arKey, uint nKeyLength, ulong h, <span>void</span> **pData);
ZEND_API <span>int</span> zend_hash_index_find(<span>const</span> HashTable *ht, ulong h, <span>void</span> **pData);
ZEND_API <span>int</span> zend_hash_exists(<span>const</span> HashTable *ht, <span>const</span><span>char</span> *arKey, uint nKeyLength);
ZEND_API <span>int</span> zend_hash_quick_exists(<span>const</span> HashTable *ht, <span>const</span><span>char</span> *arKey, uint nKeyLength, ulong h);
ZEND_API <span>int</span> zend_hash_index_exists(<span>const</span> HashTable *ht, ulong h);
ZEND_API <span>int</span> zend_hash_move_forward_ex(HashTable *ht, HashPosition *pos);
ZEND_API <span>int</span> zend_hash_move_backwards_ex(HashTable *ht, HashPosition *pos);
ZEND_API <span>int</span> zend_hash_get_current_key_ex(<span>const</span> HashTable *ht, <span>char</span> **str_index, uint *str_length, ulong *num_index, zend_bool duplicate, HashPosition *pos);
ZEND_API <span>int</span> zend_hash_get_current_key_type_ex(HashTable *ht, HashPosition *pos);
ZEND_API <span>int</span> zend_hash_get_current_data_ex(HashTable *ht, <span>void</span> **pData, HashPosition *pos);
ZEND_API <span>int</span> zend_hash_update_current_key_ex(HashTable *ht, <span>int</span> key_type, <span>const</span><span>char</span> *str_index, uint str_length, ulong num_index, <span>int</span> mode, HashPosition *pos);
ZEND_API <span>int</span> zend_hash_get_pointer(<span>const</span> HashTable *ht, HashPointer *ptr);
ZEND_API <span>int</span> zend_hash_set_pointer(HashTable *ht, <span>const</span> HashPointer *ptr);
ZEND_API <span>int</span> zend_hash_sort(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, <span>int</span> renumber TSRMLS_DC);
ZEND_API <span>int</span> zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered TSRMLS_DC);
ZEND_API <span>int</span> zend_hash_minmax(<span>const</span> HashTable *ht, compare_func_t compar, <span>int</span> flag, <span>void</span> **pData TSRMLS_DC);
ZEND_API <span>int</span> zend_hash_num_elements(<span>const</span> HashTable *ht);
ZEND_API <span>int</span> zend_hash_rehash(HashTable *ht);</code>
登入後複製

以上就介紹了php擴充開發筆記(5)一些陣列操作相關的巨集和簡單範例,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板