php扩展开发笔记(8)继承和实现接口
来源:互联网 发布:sql 2005 64 个人版 编辑:程序博客网 时间:2024/06/10 05:21
继承
我们自定义一个Exception类,php的代码:
class TestException extends Exception {}
代码实现:
#include "Zend/zend_exceptions.h"zend_class_entry *test_exception_ce;PHP_MINIT_FUNCTION(test){ zend_class_entry tmp_ce; INIT_CLASS_ENTRY(tmp_ce, "TestException", NULL); test_exception_ce = zend_register_internal_class_ex(&tmp_ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC); return SUCCESS;}
这里面主要就是实现了一个没有方法的自定义Exception类,并且继承了Exception这个类。采用了zend_register_internal_class_ex
这个带有 _ex 后缀的宏,这个宏的第二个参数zend_exception_get_default
(注意头文件)指定了父类,还可以通过类名的方式来指定父类,可以看zend_register_internal_class_ex
的定义
/* If parent_ce is not NULL then it inherits from parent_ce * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it * If both parent_ce and parent_name are NULL it does a regular class registration * If parent_name is specified but not found NULL is returned */ZEND_API zend_class_entry *zend_register_internal_class_ex(zend_class_entry *class_entry, zend_class_entry *parent_ce, char *parent_name TSRMLS_DC) /* {{{ */{ zend_class_entry *register_class; if (!parent_ce && parent_name) { zend_class_entry **pce; if (zend_hash_find(CG(class_table), parent_name, strlen(parent_name)+1, (void **) &pce)==FAILURE) { return NULL; } else { parent_ce = *pce; } } register_class = zend_register_internal_class(class_entry TSRMLS_CC); if (parent_ce) { zend_do_inheritance(register_class, parent_ce TSRMLS_CC); } return register_class;}/* }}} */
这个方法里面parent_name
传进来的时候必须是小写的,否则在寻找类名(zend_hash_find
)的时候就会失败返回NULL,例如:
test_exception_ce = zend_register_internal_class_ex(&tmp_ce, NULL, "exception" TSRMLS_CC); //如果是Exception就会继承失败
接口定义
zend_class_entry tmp_interface_ce;INIT_CLASS_ENTRY(tmp_interface_ce, "TestDataClass", test_data_interface_methodsd);test_data_interface = zend_register_internal_class(&tmp_interface_ce TSRMLS_CC);/* TestDataClass implements Countable, ArrayAccess, IteratorAggregate */zend_class_implements( test_data_interface TSRMLS_CC, 3, spl_ce_Countable, zend_ce_arrayaccess, zend_ce_aggregate);return SUCCESS;
0 0
- php扩展开发笔记(8)继承和实现接口
- PHP扩展开发-继承与接口
- 扩展实现php继承
- PHP---继承和接口
- php扩展开发笔记(6)ZVAL_STRING 和 ZVAL_STRINGL
- PHP扩展开发笔记
- 接口继承和实现继承
- 接口继承和实现继承
- 接口继承和实现继承
- 继承:继承接口和实现
- PHP实现接口多继承
- php扩展开发2(my_module实现)
- PHP扩展开发(2):实现类扩展(转载)
- PHP扩展开发(2):实现类扩展
- PHP扩展开发(2):实现类扩展
- 区分接口继承和实现继承(Effective C++_34)
- PHP 扩展开发笔记一
- php类的扩展和继承
- Scrum 扫盲篇
- SQL 数据类型:SQL Server 数据类型
- iOS之NSJSONSerialization对json解析
- java中的gui实现事件监听
- STM32F103官方初始化模板
- php扩展开发笔记(8)继承和实现接口
- Ubuntu 12.04(64bit) + CUDA-7.0 + Quadro K2200 + opencv-2.4.9
- ios修改textField的placeholder的字体颜色、大小
- 一个具有挑战性的单表查询
- JAVA 判断一个字符串是不是一个合法的日期格式
- 黑马程序员——Java面向对象之继承与多态笔记
- 1021. Deepest Root (25)
- G94 时间到数进给数
- [XCode] XCode 6给 ViewController 创建类的时候找不到 Objective C Class 选项