1. Details that need to be implemented
- Implement a Person class
- Implement a doing method and saying method
2. The first extension 2.1 creates an extension of a class:
[Email protected] ext]# Cd/usr/local/src/php-7.0.3/ext
[[email protected] ext]#./ext_skel--extname=person//Run Ext_skel Create an extended development package
2.2 Modifying the configuration
[Email protected] ext]# vim PERSON/CONFIG.M4
- DNL PHPARGwith
- DNL Make sure, the comment is aligned:
- DNL [--with-person Include person support])
- Change to:
- PHPARGwith
- DNL Make sure, the comment is aligned:
- [--with-person Include person support])
- DNL in this is the meaning of the note. Removing DNL means opening the line.
2.3 Implementation Code
Add the Php_person.h to the head.
extern zend_class_entry *person_ce; Php_method (Person_ce,__construct); Php_method (Person_ce,saying); Php_method (Person_ce, doing);
Add the PERSON.C to the head.
/*Defining Classes*/Zend_class_entry*Person_ce;/** * Declare constructor * @param * @return*/Zend_method ( person,__construct) {zend_printf ("Construct\n");}/** Declare the destructor * @param * @return*/Zend_method ( person,__destruct) {zend_printf ("Destruct\n");} Zend_method ( person,doing) {zend_printf ("Doing\n");} Zend_method ( person,saying) {zend_printf ("Saying\n");}/*NULL means no arguments are passed
*
* Zend_acc_public Description is public method
* Zend_acc_ctor Description is a constructor function
* Zend_acc_dtor description is destructor
*/ConstZend_function_entry person_functions[] ={zend_me ( person, __construct,NULL, zend_acc_public|zend_acc_ctor) Zend_me ( person, doing,NULL, Zend_acc_public) Zend_me (person,saying,NULL,zend_acc_public) Zend_me ( person, __destruct,NULL, zend_acc_public|zend_acc_dtor) Php_fe_end/*Must be the last line in person_functions[]*/};//registering classes and methods with the Zendphp_minit_function (person) {zend_class_entry CE; Init_class_entry (CE, "person",person_functions); Person_ce= Zend_register_internal_class (&CE tsrmls_cc); Zend_declare_property_null (Person_ce, "saying",strlen("saying"),zend_acc_public); Zend_declare_property_null (Person_ce, "doing",strlen("doing"),zend_acc_public); returnSUCCESS;}
Execute Command phpize
./configure
Make
make install or manual CP
Change php. ini plus [person] extenstion=person.
Using extensions:
[[email protected] tests] # cat test.php<? PHP $n New Person (); Echo $n-saying (); Echo $n, doing (); [ [email protected] tests]# php test.phpconstructsayingdoingdestruct
The article is referenced with:
Original link: http://www.djhull.com/phpext/php-ext-2.html
- Please respect the success of my work, you can reprint but keep the following information
- Years
- Date: March 2016
PHP Extension Development 2--Add Class