Comparison and introduction of related development techniques for PHP extension development

Source: Internet
Author: User
Tags php language zend
This article is to share the content of the PHP extension development of the relevant development technology comparison and introduction, has a certain reference value, the need for friends can refer to

PHP Extension is one of the skills that advanced PHP programmers must understand, how can a beginner PHP extension developer develop a mature extension into the advanced areas of PHP development? This series of development tutorials will take you from entry to the advanced stage.
This tutorial series was developed under Linux (recommended for CentOS), PHP is 5.6, and assumes you have a certain Linux experience and a C/s base.
There are problems need to communicate with friends Please add QQ Technology Group 32550793 and I communicate.

There are several technical methods and frameworks for developing PHP extensions, and for beginners, it is best to choose a framework that is the easiest to start and the fastest effect, so that you can increase your interest in learning. Here's a comparison of each technical framework, so that we can find the most suitable for their own.

First, the use of Ext-skel C language development

Ext-skel is a tool for generating PHP extensions in the official PHP source, creating a skeleton for the PHP extension of the C language framework.

The
official PHP is very unfriendly to the extension developer, the Zend API provided in the source code is extremely difficult to use, the API is complex and messy, and it is filled with various macros. Zend API pits are very large, ordinary developers can easily step into the pit. There are various puzzling core dump problems. The Zend API has virtually no documentation, and developers need to spend a lot of time learning to really master this skill.

The above is the Swoole plug-in developer's heartfelt words, can be seen using this method to develop plug-ins, for us beginners will be a very serious blow to confidence. Fortunately, the great gods have prepared for us other ways to develop PHP extensions, without learning Zend API, not proficient in C language, but also to develop PHP extensions, and the resulting expansion will not run faster than the development of C language is too much difference.

Second, the use of Zephir class PHP language development

Zephir provides a high-level language syntax similar to PHP to automatically generate extended C language code, making PHP extensions very easy to write. However, this development approach poses a problem because he does not use the C + + language development, there is no way to directly utilize the various existing C/s development libraries to achieve powerful functions. So I feel a little chicken.

Third, the use of php-x C + + language development

Php-x is a well-known swoole extension developer based on years of development experience, refining a set of extensions based on C + + development Framework. From the documentation, this is a relatively easy-to-use development framework, the data type is very complete, and PHP cpp is very similar to the development style, but I have not yet experience.
According to Php-x official documentation, the development of the extension only supports PHP7 above, which is a pity.

Iv. Development Using Phpcpp C + + language

PHP CPP is my key recommendation of the PHP Extension Development framework, simple and easy to understand, powerful, development efficiency, code easy to maintain, execute fast.

PHP cpp is a free PHP development extension Library, mainly for the C + + language, you can expand and build a collection of classes, using simple computer language, make the extension more interesting and useful, easy for developers to maintain and write, easy to understand, maintain easy and beautiful code. The algorithm written in C + + looks almost exactly the same as the algorithm written in PHP. If you know how to program in PHP, you can easily learn how to do the same in C + +.

    • Merit one: Do not need zend engine knowledge.

The interior of the Zend engine is too complex, the code of the Zend engine is a mess, and mostly undocumented. But the Php-cpp library has encapsulated all of these complex structures in very easy-to-use C + + classes and objects. You can use C + + to write amazing fast algorithms without having to call the Zend engine directly or even view the Zend engine source code. With Php-cpp, you can write native code without having to deal with the internals of PHP.

    • Advantage Two: Support all important PHP functions

With Php-cpp, you can handle variables, arrays, functions, objects, classes, interfaces, exceptions, and namespaces as easily as you would with a normal PHP script. In addition, you can use all the features of C + +, including threading, lambda and asynchronous programming.

    • Advantage Three: Support the extension development of PHP 5.X,PHP7

Php-cpp has two sets of extended development frameworks that support PHP 5.X,PHP7, although the framework code has two, but the interface is the same. So if you are going to develop a PHP extension that is compatible with multiple versions, it will not cost you much extra time to do compatibility.

Five, the development of the framework of the Hello World extension of the source of the big battle

The following is a list of the various frameworks Hello World extension source, from the source length and complexity, you can have an intuitive feeling.
The c extension source generated by Ext-skel is remarkably readable and extremely difficult to understand.
Zephir's extended source code is the most similar to PHP syntax, which is easiest to start with, but difficult to add to a mature C + + library.
Php-x and PHP CPP source code style is very similar, are standard C + + language, are easy to read. It is not difficult to imagine that these two ways to develop the expansion is necessarily the most appropriate, because we can use the C + + package to simplify the development, but also directly invoke the market of the various mature C + + libraries 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 NU LL If there ' s nothing to does at request end */Php_minfo (HelloWorld), Php_helloworld_version, Standard_module_prop Erties}; #ifdef Compile_dl_helloworldzend_get_module (HelloWorld) #endif

Zephir's Hello World source code

namespace Test;class hello{public    function say ()    {        echo "Hello world!";    }}

Php-x's Hello World source code

#include <phpx.h>using namespace std;using namespace php;//declaration function phpx_function (Say_hello);//Export Module phpx_extension ( {    Extension *ext = new Extension ("Hello-world", "0.0.1");    Ext->registerfunction (PHPX_FN (Say_hello));    return ext;} Implement function Phpx_function (Say_hello) {    echo ("Hello World");}

Hello World source code for PHP CPP

#include <phpcpp.h>void Say_hello (Php::P arameters &params) {    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;}    }

Reference documents

How to quickly develop a PHP extension based on Php-x
Php-x Chinese Help
5-minute Quick Start for PHP extension development
Zephir Chinese web
Zephir English website
Zephir Installation and demonstration development
Phpcpp English website
Phpcpp English Help
Phpcpp Chinese Help

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.