Comparison and introduction of related development technologies for PHP extension development

不言
Release: 2023-03-23 18:28:01
Original
1943 people have browsed it

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
Copy after login

zephir’s hello world source code

namespace Test;
class Hello
{
    public function say()
    {
        echo "Hello World!";
    }
}
Copy after login

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");
}
Copy after login

PHP CPP hello world source code

#include <phpcpp.h>
void say_hello(Php::Parameters &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;
    }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!