输出打印
下面3个通用输出函数,PHPWRITE()与php_write()相同,将数据写入当前输出缓冲区。 php_html_puts()通过转义HTML实体并编码其他特殊字符(如 tab和换行符)来执行其他工作,以确保非HTML格式的数据在与HTML驱动的SAPI一起使用时看起来一致。
int php_printf(const char *format, ...);
int php_write(void *buf, uint size TSRMLS_DC);
int PHPWRITE(void *buf, uint size);
void php_html_puts(const char *buf, uint size TSRMLS_DC)
标准错误
下面函数将生成标准的PHP错误消息。 请注意,此方法的所有形式都通过php_verror()进行调度。
void php_error(int type, const char *format, ...);
void php_error_docref(const char *docref TSRMLS_DC,int type, const char *format, ...);
void php_error_docref0(const char *docref TSRMLS_DC,int type, const char *format, ...);
void php_error_docref1(const char *docref TSRMLS_DC, const char *param1, int type, const char *format, ...);
void php_error_docref2(const char *docref TSRMLS_DC,const char *param1, const char *param2, int type, const char *format, ...);
void php_verror(const char *docref, const char *params, int type, const char *format, va_list args TSRMLS_DC) ;
示例:
PHP_FUNCTION(swoole_set_process_name)
{
zval *name;
long size = 128;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &name, &size) == FAILURE) {
return;
}
if (Z_STRLEN_P(name) > 127) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "process name is too long, the max length is 127");
}
...
}
异常抛出
下面函数会引发类似于从用户空间使用throw关键字调用的异常。 从内部调用此函数不会立即在下一个catch块恢复脚本执行,这意味着可能会执行额外的抛出后处理。 在内部函数将控制权返回给执行程序之后,将处理catch。
zval * zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC);
zval * zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...);
zval * zend_throw_error_exception(zend_class_entry *exception_ce, char *message, long code, int severity TSRMLS_DC);
void zend_throw_exception_object(zval *exception_obj TSRMLS_DC);
参数 | 用途 |
exception_ce | 作为类条目提供的抛出异常类型。 通常,这将使用zend_exception_get_default()或zend_get_error_exception()之一传递。 |
exception_obj | 准备好的来自Exception类的异常对象 |
code | 异常代码; 由$ e-> getCode();返回。 |
severity | 具体的错误异常类; 由$ e-> getSeverity()返回; 方法。 |
message | 以NULL结尾的异常消息 |
format | 与后续变量参数列表一起使用的sprintf样式格式参数。 |
... | 包含与sprintf样式格式说明符对应的数据的变量参数列表。 |
返回Zend异常类
返回引擎定义的异常类的类条目。 ErrorException是zend_throw_error_exception()使用的默认Exception类的子级。
zend_class_entry *zend_exception_get_default(void);
zend_class_entry *zend_get_error_exception(void);
示例:
----php7_wrapper.h----
#if PHP_MAJOR_VERSION < 7
#define sw_zend_register_internal_class_ex zend_register_internal_class_ex
#else /* PHP Version 7 */
#define sw_zend_register_internal_class_ex(entry,parent_ptr,str) zend_register_internal_class_ex(entry,parent_ptr)
#endif
----php_swoole.h----
extern zend_class_entry *swoole_exception_class_entry_ptr;
----swoole.c
zend_class_entry swoole_exception_ce;
zend_class_entry *swoole_exception_class_entry_ptr;
....
PHP_MINIT_FUNCTION(swoole)
{
...
//初始化异常类,继承自 exception 默认基类
INIT_CLASS_ENTRY(swoole_exception_ce, "swoole_exception", NULL);
swoole_exception_class_entry_ptr = sw_zend_register_internal_class_ex(&swoole_exception_ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
...
}
static PHP_METHOD(swoole_lock_wait)
{
...
if (lock->type != SW_MUTEX)
{
zend_throw_exception(swoole_exception_class_entry_ptr, "only mutex supports lockwait.", -2 TSRMLS_CC);
RETURN_FALSE;
}
...
}