1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
/* +----------------------------------------------------------------------+ | PHP Version 7 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2016 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: | +----------------------------------------------------------------------+ */ /* $Id$ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_myclass.h" static int le_myclass; //声明zend_class_entry结构体的指针,用于指向自定义类(下面会讲到) zend_class_entry * myclass_ce; //声明自定义类方法setField PHP_METHOD(myclass, setField); //指定类方法参数 //第一个参数与myclass_methods中对应 //第二、三参数填0即可 //第四个参数表示方法参数的个数 ZEND_BEGIN_ARG_INFO_EX(arginfo_myclass_setField, 0, 0, 1) //第一个参数表示是否引用传递方法参数 //第二个参数与类方法实现中的参数指针名一致 ZEND_ARG_INFO(0, fieldName) ZEND_END_ARG_INFO() //注册类方法(类名,方法名,与上面参数指定一致,访问类型) const zend_function_entry myclass_methods[] = { PHP_ME(myclass, setField, arginfo_myclass_setField, ZEND_ACC_PUBLIC) //下面这行告诉Zend引擎函数列表已完毕 {NULL, NULL, NULL} }; //实现类方法(类名,方法名) PHP_METHOD(myclass, setField) { char * fieldName=NULL; size_t fieldName_len; if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &fieldName, &fieldName_len) == FAILURE) { return; } //将参数设置为字段field的值 zend_update_property_string(myclass_ce, getThis(), "field", sizeof("field") - 1, fieldName); } PHP_MINIT_FUNCTION(myclass) { // 声明zend_class_entry结构体(zend.h中可以看到结构) zend_class_entry ce; // 初始化类 // 第一个参数:类容器(就是上面声明的结构) // 第二个参数:类名 // 第三个参数:上面注册的zend_function_entry数组,用于存放自定义方法 INIT_CLASS_ENTRY(ce, "myclass", myclass_methods); // 注册类 // 返回一个zend_class_entry的指针,指向上面声明的myclass_ce myclass_ce = zend_register_internal_class(&ce); // 定义属性 // 第一个参数:注册类后返回的zend_class_entry指针 // 第二个参数:属性名 // 第三个参数:属性名的长度 // 第四个参数:属性的访问类型 zend_declare_property_null(myclass_ce, "field", sizeof("field") - 1, ZEND_ACC_PUBLIC); return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(myclass) { return SUCCESS; } PHP_RINIT_FUNCTION(myclass) { #if defined(COMPILE_DL_MYCLASS) && defined(ZTS) ZEND_TSRMLS_CACHE_UPDATE(); #endif return SUCCESS; } PHP_RSHUTDOWN_FUNCTION(myclass) { return SUCCESS; } PHP_MINFO_FUNCTION(myclass) { php_info_print_table_start(); php_info_print_table_header(2, "myclass support", "enabled"); php_info_print_table_end(); } zend_module_entry myclass_module_entry = { STANDARD_MODULE_HEADER, "myclass", NULL, //NULL表示没有扩展函数,如果需要在类外写扩展函数,填myclass_functions,然后实现后注册它 PHP_MINIT(myclass), PHP_MSHUTDOWN(myclass), PHP_RINIT(myclass), PHP_RSHUTDOWN(myclass), PHP_MINFO(myclass), PHP_MYCLASS_VERSION, STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_MYCLASS #ifdef ZTS ZEND_TSRMLS_CACHE_DEFINE() #endif ZEND_GET_MODULE(myclass) #endif |
1 2 3 4 5 6 7 8 |
<?php //类名不区分大小写 $a = new MyClass(); //输出NULL var_dump($a->field); $a->setField("a"); //输出a var_dump($a->field); |