Home > Backend Development > PHP Tutorial > PHP extension development notes (5) Some macros and simple examples related to array operations

PHP extension development notes (5) Some macros and simple examples related to array operations

WBOY
Release: 2016-08-08 09:22:47
Original
1020 people have browsed it

To implement functions similar to isset($array[$value]), the PHP code is as follows

<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>
Copy after login

C code is as follows

<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>
Copy after login

In the above example, the zend_hash_index_exists method is mainly used. There are a series of methods for operating hash tables. 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>
Copy after login

The above introduces the PHP extension development notes (5) some macros and simple examples related to array operations, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template