Maison > développement back-end > tutoriel php > php扩展开发笔记(5)一些数组操作相关的宏和简单示例

php扩展开发笔记(5)一些数组操作相关的宏和简单示例

WBOY
Libérer: 2016-08-08 09:22:47
original
1020 Les gens l'ont consulté

实现类似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>
Copier après la connexion

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>
Copier après la connexion

上面的例子中,主要是使用 zend_hash_index_exists 方法,关于操作 hash 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>
Copier après la connexion

以上就介绍了php扩展开发笔记(5)一些数组操作相关的宏和简单示例,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal