PHP Extension Development Note (3) static array property definitions for classes

Source: Internet
Author: User
Tags php sample code

PHP Sample Code

 classslash_log {ConstEMERGENCY =1;ConstALERT =2;ConstCRITICAL =3;ConstFATAL =3;ConstERROR =4;ConstWARN =5;ConstNOTICE =6;ConstINFO =7;ConstDEBUG =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::D Ebug =8,   );}

Code implemented with PHP extensions

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);// }}}

The problem of attention

Because in the Notes (1), the extension has no way to initialize the array properties directly, so here we define a static property initialized to Null (ZEND_DECLARE_PROPERTY_NULL), then modify (zend_update_static_ property) The value of this attribute is the corresponding array, which achieves the effect of public static $array.

The corresponding variable name is defined by the macro, so you directly copy the code is not able to compile through, welcome to communicate

The above describes the PHP extension development Note (3) class static array attribute definition, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.