Statement: This article is an original article by the author, all of which have been analyzed by the author one by one. I hope you can give me some advice if there is something wrong with it. Imsiren. comarchives572 the previous chapter used extensions to create a variable .. this is a big one .. we create a class. then, call this class in php. if you do not know how to generate the extension and modify it, click here imsiren. coma
Statement: This article is an original article by the author, all of which have been analyzed by the author one by one. I hope you can give me some advice if there is something wrong with it. The last chapter of the http://imsiren.com/archives/572 creates a variable with extensions .. this is a big one .. we create a class. then, call this class in php. if you do not know how to generate extensions and modifications, click here http://imsiren.com/
Statement: This article is an original article by the author, all of which have been analyzed by the author one by one. I hope you can give me some advice if there is something wrong with it.
Http://imsiren.com/archives/572
In the previous chapter, a variable is created with an extension ..
This time, we will create a class.
Then, call this class in php.
To generate extensions and modifications not known, click here http://imsiren.com/archives/568
I will not talk about it here.
For example, we want to create a class. PHP Code as follows:
class Person { public $name; public $age; public function __construct() { echo "construct is running!"; } public function __destruct() { echo "destruct is running!"; } public function getproperty($key) { echo $this->$key; } public function setproperty($key,$val) { $this->$key = $val; }}
It's easy to use PHP ..
So what should I do with PHP extension?
OK.
1. Declare the class in php_siren.h
PHP_METHOD(Person,__construct);PHP_METHOD(Person,__destruct);PHP_METHOD(Person,setproperty);PHP_METHOD(Person,getproperty);
PHP_METHOD macro.
PHP_METHOD is equal to ZEND_METHOD
This macro accepts two parameters. The first is the class name, and the second is the class method.
# Define ZEND_METHOD (classname, name) struct (ZEND_MN (classname #### name) # define struct int ht, zval * return_value, zval ** return_value_ptr, zval * this_ptr, int return_v alue_used TSRMLS_DC // The last value is void name (int ht, zval * return_value, zval ** return_value_ptr, zval * this_ptr, int return_v alue_used TSRMLS_DC)
This macro is used to declare our method...
2. set parameters for receiving
If we need to accept parameters for our method, we need to execute
ZEND_BEGIN_ARG_INFO_EX(arg_person_info,0,0,2) ZEND_ARG_INFO(0,name)ZEND_END_ARG_INFO()
For more information, see zend_arg_info.
Typedef struct _ zend_arg_info {const char * name; // parameter name zend_uint name_len; // length const char * class_name; // class name zend_uint class_name_len; // The length of the class name zend_bool array_type_hint; zend_bool allow_null; // zend_bool pass_by_reference is allowed; // The reference value zend_bool return_reference is returned. // int limit is returned for reference; // number of parameters} zend_arg_info;
ZEND_BEGIN_ARG_INFO_EX is defined in Zend/zend_API.h.
#define ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, return_reference, required_num_args) \ static const zend_arg_info name[] = { \ { NULL, 0, NULL, 0, 0, 0, pass_rest_by_reference, return_reference, required_num_args },
It is obvious to declare an array name of zend_arg_info, and then initialize the struct value.
The definition of ZEND_ARG_INFO (0, name) is as follows:
#define ZEND_ARG_INFO(pass_by_ref, name) { #name, sizeof(#name)-1, NULL, 0, 0, 0, pass_by_ref, 0, 0 },
These three macros execute code equal
static const zend_arg_info name[] = { { NULL, 0, NULL, 0, 0, 0, pass_rest_by_reference, return_reference, required_num_args },{ #name, sizeof(#name)-1, NULL, 0, 0, 0, pass_by_ref, 0, 0 },};
3. Create a zend_function_entry structure array
const zend_function_entry person_functions[]={ PHP_ME(Person,__construct,NULL,ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) PHP_ME(Person,__destruct,NULL,ZEND_ACC_PUBLIC|ZEND_ACC_DTOR) PHP_ME(Person,getproperty,arg_person_info,ZEND_ACC_PUBLIC) PHP_ME(Person,setproperty,arg_person_info,ZEND_ACC_PUBLIC) PHP_FE_END};
Zend_function_entry is defined as follows:
Typedef struct _ zend_function_entry {const char * fname; // function name void (* handler) (handler); const struct _ zend_arg_info * arg_info; // parameter zend_uint num_args; // number of parameters zend_uint flags; // PUBLIC? PRIVATE? PROTECTED} zend_function_entry;
The PHP_ME macro receives four parameters.
Class 1 Name,
2 Method Name,
3. Parameter List of zend_arg_info,
ZEND_ACC_PUBLIC ZEND_ACC_PRIVATE ZEND_ACC_PROTECTED is the three access permissions in our class.
ZEND_ACC_CTOR Constructor
ZEND_ACC_DTOR indicates the destructor
4. Modify PHP_MINIT_FUNCTION
We mentioned earlier that PHP_MINIT_FUNCTION is a function executed when the module is started.
First create a global pointer zend_class_entry * person_ce;
Add the following code to PHP_MINIT_FUNCTION:
zend_class_entry person;INIT_CLASS_ENTRY(person,"Person",person_functions);person_ce=zend_register_internal_class_ex(&person,NULL,NULL TSRMLS_CC);zend_declare_property_null(person_ce,ZEND_STRL("name"),ZEND_ACC_PUBLIC TSRMLS_CC);
Create a zend_class_entry object person in line 1.
The structure zend_class_entry has previously discussed implementation of PHP kernel research and so on.
Two lines initialize zend_class_entry. It executes the following code:
{ \ int _len = class_name_len; \ class_container.name = zend_strndup(class_name, _len); \ class_container.name_length = _len; \ class_container.builtin_functions = functions; \ class_container.constructor = NULL; \ class_container.destructor = NULL; \ class_container.clone = NULL; \ class_container.serialize = NULL; \ class_container.unserialize = NULL; \ class_container.create_object = NULL; \ class_container.interface_gets_implemented = NULL; \ class_container.get_static_method = NULL; \ class_container.__call = handle_fcall; \ class_container.__callstatic = NULL; \ class_container.__tostring = NULL; \ class_container.__get = handle_propget; \ class_container.__set = handle_propset; \ class_container.__unset = handle_propunset; \ class_container.__isset = handle_propisset; \ class_container.serialize_func = NULL; \ class_container.unserialize_func = NULL; \ class_container.serialize = NULL; \ class_container.unserialize = NULL; \ class_container.parent = NULL; \ class_container.num_interfaces = 0; \ class_container.interfaces = NULL; \ class_container.get_iterator = NULL; \ class_container.iterator_funcs.funcs = NULL; \ class_container.module = NULL; \ }
It can be analyzed based on implementations such as PHP kernel research.
5. Create the method body in the php_siren.h header file
PHP_METHOD(Person,__construct){ php_printf("construct is running
");}PHP_METHOD(Person,__destruct){ php_printf("destruct is running
");}PHP_METHOD(Person,setproperty){}PHP_METHOD(Person,getproperty){}
6. make & make install
Compile our extensions,
Restart apache.
$ P = new Person ();
?>
We can see the output content in the browser.
Construct is running
Destruct is running
In this way, a basic class created with extension is complete.