


Comparison and introduction of related development technologies for PHP extension development
The content of this article is to share with you the comparison and introduction of related development technologies for PHP extension development. It has a certain reference value. Friends in need can refer to it
PHP extension is a must for advanced PHP programmers One of the skills to understand is that for a beginning PHP extension developer, how can he develop a mature extension and enter the advanced field of PHP development? This series of development tutorials will take you step by step from entry to advanced stages.
This tutorial series is developed under Linux (centos is recommended), the PHP version is 5.6, and it is assumed that you have certain Linux operating experience and C/C foundation.
If you have any questions and need to communicate, please join the QQ technical exchange group 32550793 to communicate with me.
There are several technical methods and frameworks for developing PHP extensions. For beginners, it is best to choose a framework that is easiest to get started and produces the fastest results, so as to increase interest in learning. Let’s compare each technical framework one by one so that everyone can find the one that suits them best.
1. Use ext-skel C language development
ext-skel is a tool for generating PHP extensions provided in the PHP official source code. It can generate a PHP extension skeleton of a C language framework.
PHP is officially very unfriendly to extension developers. The Zend API provided in the source code is extremely difficult to use. The API is complex and messy, and is full of various macro writing methods. There are many pitfalls in the Zend API, and ordinary developers can easily fall into them. Various inexplicable core dump problems occur. The Zend API has almost no documentation, and developers need to spend a lot of time learning if they want to truly master this skill.
The above are the heartfelt words of the swoole plug-in developer. It can be seen that using this method to develop plug-ins will be a serious blow to our self-confidence as beginners. Fortunately, some masters have prepared other methods for developing PHP extensions for us. We don’t need to learn ZEND API or be proficient in C language, and we can still develop PHP extensions, and the running speed of the generated extensions will not be much different than those developed in C language.
2. Use Zephir PHP-like language development
Zephir provides a high-level language syntax similar to PHP to automatically generate extended C language code, making writing PHP extensions very easy. of simplicity. However, this development method brings a problem, that is, because it is not developed in C/C language, there is no way to directly use various existing C/C development libraries to achieve powerful functions. So it feels a bit tasteless.
3. Use PHP-X C language development
php-x is a set of C-based extension development framework refined by the well-known swoole extension developer based on years of development experience. Judging from the documentation, this is a relatively easy-to-use development framework with complete data types. It is very similar to the development style of php cpp, but I have not experienced it yet.
According to the official php-x documentation, the developed extension only supports PHP7 and above, which is a pity.
4. Use phpcpp C language development
PHP CPP is the PHP extension development framework that I highly recommend. It is simple and easy to understand, has powerful functions, high development efficiency, easy code maintenance, and fast execution speed.
PHP CPP is a free PHP development extension library, mainly for C language. It can extend and build class collections. It uses simple computer language to make extensions more interesting and useful, and convenient for developers. Maintain and write code that is easy to understand, effortless to maintain, and beautiful in code. An algorithm written in C looks almost identical to an algorithm written in PHP. If you know how to program in PHP, you can easily learn how to do the same in C.
Advantage 1: No knowledge of Zend engine is required.
The internals of the Zend engine are too complex, the code of the Zend engine is a mess, and most of it is undocumented. But the PHP-CPP library has encapsulated all these complex structures in very easy-to-use C classes and objects. You can write amazingly fast algorithms in C without having to call the Zend Engine directly or even look at the Zend Engine source code. With PHP-CPP you can write native code without having to deal with PHP's internals.
Advantage 2: Supports all important PHP features
With PHP-CPP, you can handle variables as easily as with normal PHP scripts , arrays, functions, objects, classes, interfaces, exceptions and namespaces. In addition to this, you can use all the features of C, including threads, lambdas and asynchronous programming.
Advantage Three: Support PHP 5.X, PHP7 extension development
PHP-CPP has two sets of extension development frameworks, supporting PHP respectively 5.X, PHP7, although there are two framework codes, the interfaces are the same. So if you want to develop a PHP extension that is compatible with multiple versions, it won't cost you much extra time to make it compatible.
5. Competition of hello world extension source codes of various development frameworks
The hello world extension source codes of each framework are listed below. From the length and complexity of the source code, you can have an intuitive feeling.
The c extension source code generated by ext-skel is obviously very poorly readable and extremely difficult to understand.
zephir's extended source code is most similar to PHP syntax and is the easiest to start with, but it is difficult to add mature c/c library code.
The source code styles of PHP-X and PHP CPP are very similar. They are both in standard C language and are easy to understand. It is not difficult to imagine that these two methods of developing extensions must be the most suitable, because we can not only use C encapsulation to simplify development, but also directly call various mature C libraries on the market to serve us.
ext-skel’s hello world source code
#ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_helloworld.h" static int le_helloworld; PHP_FUNCTION(confirm_helloworld_compiled) { char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } len = spprintf(&strg, 0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "helloworld", arg); RETURN_STRINGL(strg, len, 0); } PHP_MINIT_FUNCTION(helloworld) { return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(helloworld) { return SUCCESS; } PHP_RINIT_FUNCTION(helloworld) { return SUCCESS; } PHP_RSHUTDOWN_FUNCTION(helloworld) { return SUCCESS; } PHP_MINFO_FUNCTION(helloworld) { php_info_print_table_start(); php_info_print_table_header(2, "helloworld support", "enabled"); php_info_print_table_end(); } const zend_function_entry helloworld_functions[] = { PHP_FE(confirm_helloworld_compiled, NULL) /* For testing, remove later. */ PHP_FE_END /* Must be the last line in helloworld_functions[] */ }; zend_module_entry helloworld_module_entry = { STANDARD_MODULE_HEADER, "helloworld", helloworld_functions, PHP_MINIT(helloworld), PHP_MSHUTDOWN(helloworld), PHP_RINIT(helloworld), /* Replace with NULL if there's nothing to do at request start */ PHP_RSHUTDOWN(helloworld), /* Replace with NULL if there's nothing to do at request end */ PHP_MINFO(helloworld), PHP_HELLOWORLD_VERSION, STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_HELLOWORLD ZEND_GET_MODULE(helloworld) #endif
zephir’s hello world source code
namespace Test; class Hello { public function say() { echo "Hello World!"; } }
PHP-X hello world source code
#include <phpx.h> using namespace std; using namespace php; //声明函数 PHPX_FUNCTION(say_hello); //导出模块 PHPX_EXTENSION() { Extension *ext = new Extension("hello-world", "0.0.1"); ext->registerFunction(PHPX_FN(say_hello)); return ext; } //实现函数 PHPX_FUNCTION(say_hello) { echo("hello world"); }
PHP CPP hello world source code
#include <phpcpp.h> void say_hello(Php::Parameters ¶ms) { Php::out << "hello world" << std::endl; } extern "C" { PHPCPP_EXPORT void *get_module() { static Php::Extension extension("helloworld", "1.0"); extension.add("say_hello", say_hello); return extension; } }
References
How to quickly develop a PHP based on PHP-X Extension
PHP-X Chinese Help
5-minute PHP extension development quick start
zephir Chinese website
zephir English official website
zephir installation and demonstration development
phpcpp English official website
phpcpp English Help
phpcpp Chinese help
The above is the detailed content of Comparison and introduction of related development technologies for PHP extension development. 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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.
