PHP Extension Development Notes (8) inheriting and implementing interfaces

Source: Internet
Author: User

Inherited

We customize a exception class, PHP code:

classTestExceptionextendsException {}

Code implementation:

#include "Zend/zend_exceptions.h"zend_class_entry *test_exception_ce;PHP_MINIT_FUNCTION(test){    zend_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;}

The main thing is to implement a custom exception class without a method, and inherit the exception class. Using zend_register_internal_class_ex this macro with the _EX suffix, the second parameter of the macro zend_exception_get_default (note header file) Specifies the parent class, you can also specify the parent class by the way of the class name, you can see zend_register_internal_class_ex the definition

/ * If PARENT_CE is isn't null then it inherits from Parent_ce * If parent_ce are NULL and parent_name isn ' t then it looks For the parent and inherits from it * If both Parent_ce and Parent_name is NULL it does a regular class registration * I F parent_name is specified and not found NULL is returned * /Zend_api zend_class_entry *zend_register_internal_class_ex (zend_class_entry *class_entry, Zend_class_entry *parent_ CeChar*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) {returnNULL; }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); }returnRegister_class;}/* }}} */

This method must be in parent_name lowercase when it is passed in, otherwise zend_hash_find it will fail to return NULL when looking for the class name (), for example:

"exception"//如果是Exception就会继承失败

Interface definition

"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(    3, spl_ce_Countable, zend_ce_arrayaccess, zend_ce_aggregate);return SUCCESS;

The above describes the PHP extension development Note (8) to inherit and implement the interface, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.