首页 / PHP / php扩展开发笔记(8)继承和实现接口
php扩展开发笔记(8)继承和实现接口
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php扩展开发笔记(8)继承和实现接口,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2277字,纯文字阅读大概需要4分钟。
内容图文
![php扩展开发笔记(8)继承和实现接口](/upload/InfoBanner/zyjiaocheng/155/3165717694084c67a50b962490cbae9d.jpg)
继承
我们自定义一个Exception类,php的代码:
classTestExceptionextendsException {}
代码实现:
#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;
以上就介绍了php扩展开发笔记(8)继承和实现接口,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
内容总结
以上是互联网集市为您收集整理的php扩展开发笔记(8)继承和实现接口全部内容,希望文章能够帮你解决php扩展开发笔记(8)继承和实现接口所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。