欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-01-12 22:37:46
...

实现类似isset($array[$value]) 功能,php代码如下

classSlash_Log {protectedstatic$levels = array(
        1 => "ERROR",
        2 => "WARNING",
        3 => "INFO",
        4 => "DEBUG",
    );

    publicfunctionsetLevel($level) {if (!isset(self::$levels)) {
            thrownewException("Level is not allowed");
        }
    }
}

C 代码如下

PHP_METHOD(slash_log, setLevel) {

    long level;
    zval *levels;
    zend_class_entry *ce;
    HashTable *levels_ht;

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

    levels = zend_read_static_property(ce, ZEND_STRL(SL_P_LEVELS), 0 TSRMLS_DC);
    levels_ht = Z_ARRVAL_P(levels);

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

    zend_update_property_long(ce, getThis(), ZEND_STRL(SL_P_LEVEL), level TSRMLS_CC);
    RETURN_TRUE;
}

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

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

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