search
HomeBackend DevelopmentPHP TutorialComparison 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 &params)
{
    Php::out <h2 id="References">References</h2><p>How to quickly develop a PHP based on PHP-X Extension<br>PHP-X Chinese Help<br>5-minute PHP extension development quick start<br>zephir Chinese website<br>zephir English official website<br>zephir installation and demonstration development<br>phpcpp English official website<br>phpcpp English Help<br>phpcpp Chinese help</p><p><br></p></phpcpp.h>

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
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
How can you prevent session fixation attacks?How can you prevent session fixation attacks?Apr 28, 2025 am 12:25 AM

Effective methods to prevent session fixed attacks include: 1. Regenerate the session ID after the user logs in; 2. Use a secure session ID generation algorithm; 3. Implement the session timeout mechanism; 4. Encrypt session data using HTTPS. These measures can ensure that the application is indestructible when facing session fixed attacks.

How do you implement sessionless authentication?How do you implement sessionless authentication?Apr 28, 2025 am 12:24 AM

Implementing session-free authentication can be achieved by using JSONWebTokens (JWT), a token-based authentication system where all necessary information is stored in the token without server-side session storage. 1) Use JWT to generate and verify tokens, 2) Ensure that HTTPS is used to prevent tokens from being intercepted, 3) Securely store tokens on the client side, 4) Verify tokens on the server side to prevent tampering, 5) Implement token revocation mechanisms, such as using short-term access tokens and long-term refresh tokens.

What are some common security risks associated with PHP sessions?What are some common security risks associated with PHP sessions?Apr 28, 2025 am 12:24 AM

The security risks of PHP sessions mainly include session hijacking, session fixation, session prediction and session poisoning. 1. Session hijacking can be prevented by using HTTPS and protecting cookies. 2. Session fixation can be avoided by regenerating the session ID before the user logs in. 3. Session prediction needs to ensure the randomness and unpredictability of session IDs. 4. Session poisoning can be prevented by verifying and filtering session data.

How do you destroy a PHP session?How do you destroy a PHP session?Apr 28, 2025 am 12:16 AM

To destroy a PHP session, you need to start the session first, then clear the data and destroy the session file. 1. Use session_start() to start the session. 2. Use session_unset() to clear the session data. 3. Finally, use session_destroy() to destroy the session file to ensure data security and resource release.

How can you change the default session save path in PHP?How can you change the default session save path in PHP?Apr 28, 2025 am 12:12 AM

How to change the default session saving path of PHP? It can be achieved through the following steps: use session_save_path('/var/www/sessions');session_start(); in PHP scripts to set the session saving path. Set session.save_path="/var/www/sessions" in the php.ini file to change the session saving path globally. Use Memcached or Redis to store session data, such as ini_set('session.save_handler','memcached'); ini_set(

How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

See all articles

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

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function