php擴充開發筆記(3)類別的靜態陣列屬性定義

WBOY
發布: 2016-08-08 09:23:03
原創
1328 人瀏覽過

php範例程式碼

<code><span><span>class</span><span>Slash_Log</span> {</span><span>const</span> EMERGENCY = <span>1</span>;
    <span>const</span> ALERT = <span>2</span>;
    <span>const</span> CRITICAL = <span>3</span>;
    <span>const</span> FATAL = <span>3</span>;
    <span>const</span> ERROR = <span>4</span>;
    <span>const</span> WARN = <span>5</span>;
    <span>const</span> NOTICE = <span>6</span>;
    <span>const</span> INFO = <span>7</span>;
    <span>const</span> DEBUG = <span>8</span>;

    <span>public</span><span>static</span><span>$levels</span> = <span>array</span>(
        <span>self</span>::EMERGENCY => <span>1</span>,
        <span>self</span>::ALERT => <span>2</span>,
        <span>self</span>::CRITICAL => <span>3</span>,
        <span>self</span>::FATAL => <span>3</span>,
        <span>self</span>::ERROR => <span>4</span>,
        <span>self</span>::WARN => <span>5</span>,
        <span>self</span>::NOTICE => <span>6</span>,
        <span>self</span>::INFO => <span>7</span>,
        <span>self</span>::DEBUG => <span>8</span>,
   );
}</code>
登入後複製

透過php擴充實現的程式碼

<code>zend_class_entry ce;
SLASH_INIT_CLASS_ENTRY(ce, SLASH_CN_LOG, slash_log_methods);
slash_log_ce = zend_register_internal_class(&ce TSRMLS_CC);

<span>// {{{ log level</span>
zend_declare_class_constant_long(slash_log_ce, ZEND_STRL(SL_EMERGENCY_K), SL_EMERGENCY_V TSRMLS_CC);
zend_declare_class_constant_long(slash_log_ce, ZEND_STRL(SL_ALERT_K), SL_ALERT_V TSRMLS_CC);
zend_declare_class_constant_long(slash_log_ce, ZEND_STRL(SL_CRITICAL_K), SL_CRITICAL_V TSRMLS_CC);
zend_declare_class_constant_long(slash_log_ce, ZEND_STRL(SL_ERROR_K), SL_ERROR_V TSRMLS_CC);
zend_declare_class_constant_long(slash_log_ce, ZEND_STRL(SL_WARN_K), SL_WARN_V TSRMLS_CC);
zend_declare_class_constant_long(slash_log_ce, ZEND_STRL(SL_NOTICE_K), SL_NOTICE_V TSRMLS_CC);
zend_declare_class_constant_long(slash_log_ce, ZEND_STRL(SL_INFO_K), SL_INFO_V TSRMLS_CC);
zend_declare_class_constant_long(slash_log_ce, ZEND_STRL(SL_DEBUG_K), SL_DEBUG_V TSRMLS_CC);
<span>// }}}</span><span>// {{{ static property array</span>
zend_declare_property_null(slash_log_ce, ZEND_STRL(SL_P_LEVELS), ZEND_ACC_PUBLIC|ZEND_ACC_STATIC TSRMLS_CC);

zval *levels;
MAKE_STD_ZVAL(levels);
array_init(levels);

add_index_stringl(levels, SL_EMERGENCY_V, ZEND_STRL(SL_EMERGENCY_K), <span>0</span>);
add_index_stringl(levels, SL_ALERT_V, ZEND_STRL(SL_ALERT_K), <span>0</span>);
add_index_stringl(levels, SL_CRITICAL_V, ZEND_STRL(SL_CRITICAL_K), <span>0</span>);
add_index_stringl(levels, SL_ERROR_V, ZEND_STRL(SL_ERROR_K), <span>0</span>);
add_index_stringl(levels, SL_WARN_V, ZEND_STRL(SL_WARN_K), <span>0</span>);
add_index_stringl(levels, SL_NOTICE_V, ZEND_STRL(SL_NOTICE_K), <span>0</span>);
add_index_stringl(levels, SL_INFO_V, ZEND_STRL(SL_INFO_K), <span>0</span>);
add_index_stringl(levels, SL_DEBUG_V, ZEND_STRL(SL_DEBUG_K), <span>0</span>);
zend_update_static_property(slash_log_ce, ZEND_STRL(SL_P_LEVELS), levels TSRMLS_CC);
<span>// }}}</span></code>
登入後複製

注意的問題

因為在筆記(1)說過了,擴充沒辦法直接初始化陣列屬性,所以這裡我們定義一個靜態的屬性初始化為null(zend_declare_property_null),之後去修改(zend_update_static_property)這個屬性的值為對應的數組,就達到了public static $array 的效果。

對應的變數名字是定義了宏,所以你直接copy程式碼沒辦法編譯通過,歡迎交流

以上就介紹了php擴充開發筆記(3)類別的靜態陣列屬性定義,包含了面向的內容,希望對PHP教學有興趣的朋友有幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!