Maison  >  Article  >  développement back-end  >  php扩展开发笔记(8)继承和实现接口

php扩展开发笔记(8)继承和实现接口

WBOY
WBOYoriginal
2016-08-08 09:22:231058parcourir

继承

我们自定义一个Exception类,php的代码:

<code><span><span>class</span><span>TestException</span><span>extends</span><span>Exception</span> {</span>}</code>

代码实现:

<code><span>#include "Zend/zend_exceptions.h"</span>
zend_class_entry *test_exception_ce;
PHP_MINIT_FUNCTION(test)
{
    zend_class_entry tmp_ce;
    INIT_CLASS_ENTRY(tmp_ce, <span>"TestException"</span>, NULL);
    test_exception_ce = zend_register_internal_class_ex(&tmp_ce, zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);

    <span>return</span> SUCCESS;
}</code>

这里面主要就是实现了一个没有方法的自定义Exception类,并且继承了Exception这个类。采用了zend_register_internal_class_ex 这个带有 _ex 后缀的宏,这个宏的第二个参数zend_exception_get_default(注意头文件)指定了父类,还可以通过类名的方式来指定父类,可以看zend_register_internal_class_ex 的定义

<code><span>/* 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
 */</span>
ZEND_API zend_class_entry *zend_register_internal_class_ex(zend_class_entry *class_entry, zend_class_entry *parent_ce, <span>char</span> *parent_name TSRMLS_DC) <span>/* {{{ */</span>
{
    zend_class_entry *register_class;

    <span>if</span> (!parent_ce && parent_name) {
        zend_class_entry **pce;
        <span>if</span> (zend_hash_find(CG(class_table), parent_name, <span>strlen</span>(parent_name)+<span>1</span>, (<span>void</span> **) &pce)==FAILURE) {
            <span>return</span> NULL;
        } <span>else</span> {
            parent_ce = *pce;
        }
    }

    register_class = zend_register_internal_class(class_entry TSRMLS_CC);

    <span>if</span> (parent_ce) {
        zend_do_inheritance(register_class, parent_ce TSRMLS_CC);
    }
    <span>return</span> register_class;
}
<span>/* }}} */</span></code>

这个方法里面parent_name 传进来的时候必须是小写的,否则在寻找类名(zend_hash_find)的时候就会失败返回NULL,例如:

<code>test_exception_ce = zend_register_internal_class_ex(&tmp_ce, NULL, <span>"exception"</span> TSRMLS_CC); <span>//如果是Exception就会继承失败</span></code>

接口定义

<code>zend_class_entry tmp_interface_ce;
INIT_CLASS_ENTRY(tmp_interface_ce, <span>"TestDataClass"</span>, test_data_interface_methodsd);
test_data_interface = zend_register_internal_class(&tmp_interface_ce TSRMLS_CC);

<span>/* TestDataClass implements Countable, ArrayAccess, IteratorAggregate */</span>
zend_class_implements(
    test_data_interface TSRMLS_CC, <span>3</span>, spl_ce_Countable, zend_ce_arrayaccess, zend_ce_aggregate
);

<span>return</span> SUCCESS;</code>

以上就介绍了php扩展开发笔记(8)继承和实现接口,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Article précédent:PHP设计模式——单例模式Article suivant:centos 安装 pear