Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to write a PHP extension under Linux

Shulou Source: shulou.com Published: 2022-06-02 04:21:47 09月29日 Update

This article mainly explains "how to write a PHP extension under Linux". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "how to write a PHP extension under Linux" together!

hypothetical demand

Develop an extension called helloWord.

There is a function called helloWord().

echo helloWord ('Tom ');//returns: Hello World: Tom

local environment

PHP version: 5.6.9

Linux CentOS release 6.5 (Final)

final effect

realization flow

Step 1:

Go to the local php directory and execute: cd /root/soft/src/php-5.6.9cd ext./ ext_skel --extname=helloWordcd helloWordvi config.m4 Search: dnl Otherwise use enable Modify the following to: PHP_ARG_ENABLE(helloWorld, whether to enable helloWorld support,[ --enable-helloWorld Enable helloWorld support])if test "$PHP_HELLOWORLD" != "no"; then...

As shown in the figure:

Step 2:

vi php_helloWorld.h Search: extern zend_module_entry Add a new line: PHP_FUNCTION(helloWorld);

As shown in the figure:

Step 3:

vi helloWorld.c Search: const zend_function_entry helloWorld_functions[] Add a new line: PHP_FE(helloWorld, NULL)

As shown in the figure:

Add a method PHP_FUNCTION(helloWorld){ 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, "Hello World: %s", arg); RETURN_STRINGL(strg, len, 0);}

As shown in the figure:

Step 4:

//compile install cd /root/soft/src/php-5.6.9/ext/usr/local/php/bin/phpize #generate configure configuration file with phpize./ configure --with-php-config=/usr/local/php/bin/php-config #configure make #compile make install #install

Step 5:

//modify php.iniextension="helloWorld.so" #Name is the name of the installation extension

Step 6:

Restart the environment.

After completing the above steps, the simple helloWorld extension is OK.

Thank you for reading, the above is "how to write a PHP extension under Linux" content, after the study of this article, I believe we have a deeper understanding of how to write a PHP extension under Linux, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Tags: The following part such as figure learning search line content name environment compilation configuration function that is bottom idea situation effect file article method more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology OPPO Reno Shulou Tech Info vpn Docker