当前位置:Gxlcms > PHP教程 > php扩展开发笔记(3)类的静态数组属性定义

php扩展开发笔记(3)类的静态数组属性定义

时间:2021-07-01 10:21:17 帮助过:36人阅读

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 level
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);
// }}}// {{{ static property array
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), 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教程有兴趣的朋友有所帮助。

人气教程排行