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>
<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 메소드를 주로 사용합니다. 해시 테이블에는 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 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.