Skip to content

Instantly share code, notes, and snippets.

@ixqbar
Last active August 29, 2015 14:18
Show Gist options
  • Save ixqbar/66d229317f8cceddf74f to your computer and use it in GitHub Desktop.
Save ixqbar/66d229317f8cceddf74f to your computer and use it in GitHub Desktop.
php自定义异常类
zend_class_entry *wcx_exception_ce;
PHP_WCX_API zend_class_entry *wcx_get_exception_base(int root TSRMLS_DC) {
#if HAVE_SPL
if (!root) {
if (!spl_ce_RuntimeException) {
zend_class_entry **pce;
if (zend_hash_find(CG(class_table), "runtimeexception", sizeof("RuntimeException"), (void **) &pce) == SUCCESS) {
spl_ce_RuntimeException = *pce;
return *pce;
}
} else {
return spl_ce_RuntimeException;
}
}
#endif
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 2)
return zend_exception_get_default();
#else
return zend_exception_get_default(TSRMLS_C);
#endif
}
PHP_MINIT_FUNCTION(wcx) {
zend_class_entry wcx_exception_class_entry;
INIT_CLASS_ENTRY(wcx_exception_class_entry, "WcxException", NULL);
wcx_exception_ce = zend_register_internal_class_ex(
&wcx_exception_class_entry,
wcx_get_exception_base(0 TSRMLS_CC),
NULL TSRMLS_CC
);
zend_declare_property_null(wcx_exception_ce, ZEND_STRL("message"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_long(wcx_exception_ce, ZEND_STRL("code"), 0, ZEND_ACC_PROTECTED TSRMLS_CC);
...
}
//usage
zend_throw_exception(wcx_exception_ce, message, code TSRMLS_CC);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment