In php7, there are many extension classes. Today we will take the person class as an example to implement the doing method and saying method. Friends in need can refer to it.
1. Details that need to be implemented
Implement a person class
Implement a doing method and a saying method
2. The first extension
2.1 Create an extension of the class:
[root@bogon ext]# cd /usr/local/src/php-7.0.3/ext [root@bogon ext]# ./ext_skel --extname=person
2.2 Modify the configuration
[root@bogon ext]# vim person/config.m4 dnl PHPARGWITH(person, for person support, dnl Make sure that the comment is aligned: dnl [ --with-person Include person support])
to:
PHPARGWITH(person, for person support, dnl Make sure that the comment is aligned: [ --with-person Include person support])
2.3 Implementation code
Add
extern zend_class_entry *person_ce; PHP_METHOD(person_ce,__construct); PHP_METHOD(person_ce,saying); PHP_METHOD(person_ce,doing);
to the php_person.h header. Add
/** * 声明构造函数 * @param * @return */ ZEND_METHOD(person,__construct){ zval *pThis; pThis = getThis(); zend_printf("construct\n"); } /** * 声明析造函数 * @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"); } //这个函数需要加上声明,去掉了没用的test函数const zend_function_entry person_functions[] = { ZEND_ME(person, __construct, global_config_arg, 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[] */ }; //将类和方法注册到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); return SUCCESS; }
to the person.c header. 2.4 Compile
* [root@bogon hello]# [root@localhost person]# ./configure && make && make install
2.5 Extension installation
Change php.ini and add
[person] extenstion=person.so
2.6 Extended use
[root@bogon tests]# cat test.php <?php $n = new person(); echo $n->saying(); echo $n->doing(); [root@localhost tests]# php test.phpconstructsayingdoingdestruct
Recommended learning: php video tutorial
The above is the detailed content of How to write php7 extension class. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software