php擴充開發筆記(3)類的靜態數組屬性定義

來源:互聯網
上載者:User

php範例程式碼

classSlash_Log {const EMERGENCY = 1;    const ALERT = 2;    const CRITICAL = 3;    const FATAL = 3;    const ERROR = 4;    const WARN = 5;    const NOTICE = 6;    const INFO = 7;    const DEBUG = 8;    publicstatic$levels = array(        self::EMERGENCY => 1,        self::ALERT => 2,        self::CRITICAL => 3,        self::FATAL => 3,        self::ERROR => 4,        self::WARN => 5,        self::NOTICE => 6,        self::INFO => 7,        self::DEBUG => 8,   );}

通過php擴充來實現的代碼

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);// {{{ log levelzend_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);// }}}// {{{ static property arrayzend_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), 0);add_index_stringl(levels, SL_ALERT_V, ZEND_STRL(SL_ALERT_K), 0);add_index_stringl(levels, SL_CRITICAL_V, ZEND_STRL(SL_CRITICAL_K), 0);add_index_stringl(levels, SL_ERROR_V, ZEND_STRL(SL_ERROR_K), 0);add_index_stringl(levels, SL_WARN_V, ZEND_STRL(SL_WARN_K), 0);add_index_stringl(levels, SL_NOTICE_V, ZEND_STRL(SL_NOTICE_K), 0);add_index_stringl(levels, SL_INFO_V, ZEND_STRL(SL_INFO_K), 0);add_index_stringl(levels, SL_DEBUG_V, ZEND_STRL(SL_DEBUG_K), 0);zend_update_static_property(slash_log_ce, ZEND_STRL(SL_P_LEVELS), levels TSRMLS_CC);// }}}

注意的問題

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

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

以上就介紹了php擴充開發筆記(3)類的靜態數組屬性定義,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.