Next we will implement the following classes in the form of Extensions (Demo environment: Linux, PHP-5.5.34-SRC)
1<?PHP2 class Person3 {4 Private$_name;5 Publicfunction GetName ()6 {7 return$ This-_name;8 } 9 Publicfunction SetName ($name)Ten { One$ This-_name =$name; A } -}
Under the PHP source directory
1 cd php-5.5. -src2CD ext3 ./ext_skel--EXTNAME=HDX // generate HDX extension skeleton 4 CD HDX
1. Modify CONFIG.M4
for HDX Support, [ --with-HDX Include HDX support]) // Remove the DNL from the front of the two lines in about 10-12 lines .
2. Add the following in Hdx.h
Php_method (person, __construct); Php_method (person, __destruct); Php_method (person, getName); Php_method (person, setName);
3. Add the appropriate function below HDX.C
1 Php_method (person, __construct) {2php_printf ("__construct called.");3 }4 5 Php_method (person, __destruct) {6php_printf ("__destruct called.<br/>");7 }8 9 Php_method (person, getName) {TenZval *self, *name; OneSelf =getthis (); AName = Zend_read_property (Z_objce_p (self), self, Zend_strl ("_name"),0tsrmls_cc); -Return_string (z_strval_p (name),0); - } the - Php_method (person, setName) { - Char*arg =NULL; - intArg_len; +Zval *value, *Self ; - if(Zend_parse_parameters (Zend_num_args () TSRMLS_CC,"s", &arg, &arg_len) = =FAILURE) { + Wrong_param_count; A } atSelf =getthis (); - Make_std_zval (value); -Zval_stringl (value, ARG, Arg_len,0); -Separate_zval_to_make_is_ref (&value); -Zend_update_property (Z_objce_p (self), self, Zend_strl ("_name"), value tsrmls_cc); - return_true; in}
Do some explaining to the above code:
A. Get the parameter information of the method, still use the Zend_parse_parameters function, as we have described before;
B. Get the This pointer (as opposed to PHP code, still using the zval structure representation in PHP extensions) using the Getthis () function;
C. Use the Make_std_zval macro to request and initialize a zval structure, in the PHP extension, all data types are actually represented by the zval structure, in this series of articles I will write a separate article to introduce Zval.
D. Get property values use the Zend_read_property () function to update the property values with the Zend_update_property () function.
4. Above hdx.c, initialize the class: in the extended initialization function, register and initialize the class.
1Zend_class_entry *Person_ce;2 3 php_minit_function (Fetion_echo)4 { 5Zend_class_entry person;
Init_class_entry (Person," Person", fetion_echo_functions);6Person_ce = ZEND_REGISTER_INTERNAL_CLASS_EX (&Person , NULL, null TSRMLS_CC);7 8Zend_declare_property_null (Person_ce, Zend_strl ("_name"), Zend_acc_private tsrmls_cc);returnSUCCESS;9}
Initializes the class with the Init_class_entry macro, the second parameter specifies the class name, and the third parameter is the function table.
5. Register to function: Declare the parameters of the method and register it in the function table.
1Zend_begin_arg_info (Arg_person_setname,0)2Zend_arg_info (0, name)3Zend_end_arg_info ()ConstZend_function_entry fetion_echo_functions[] = {4Php_me (person, __construct, NULL, zend_acc_public|zend_acc_ctor)5Php_me (person, __destruct, NULL, zend_acc_public|zend_acc_dtor)6 Php_me (person, GetName, NULL, Zend_acc_public)7 Php_me (person, SetName, Arg_person_setname, Zend_acc_public)8{null, NULL, NULL}/*Must be the last line in fetion_echo_functions[]*/};
The declaration of a class method parameter is consistent with the way in which we previously declared the function parameter, using the PHP_ME macro when registering the class method into the function table, rather than the Php_fe macro that was previously used.
Zend_acc_public: The access modifier for the specified method
Zend_acc_ctor: Specifies that the method is a constructor
Zend_acc_dtor: Specifies that this method is a destructor
6. Installation
1 /usr/local/php/bin/phpize2 ./configure--with-php-config=/usr/local/php/bin/php- Config3 make && make install
Then modify the php.ini in the PHP installation directory to add the extension
7. Run the test: after compiling and installing the extension, write a simple test script:
1 <? 2new person (); 3 $person->setname ("Huangdongxi"4 echo $person->getname (). <br/> ';
After running, you can see the following output, indicating that the extension is working properly:
__construct called.
Huangdongxi
__destruct called.
Simple class development of PHP extension development