Table of Contents
1. Use ext-skel C language development
2. Use Zephir PHP-like language development
3. Use PHP-X C language development
4. Use phpcpp C language development
5. Competition of hello world extension source codes of various development frameworks
References
Home Backend Development PHP Tutorial Comparison and introduction of related development technologies for PHP extension development

Comparison and introduction of related development technologies for PHP extension development

Apr 13, 2018 pm 04:13 PM
php Compared Development Technology

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles