开发者

How to make a PHP extension [duplicate]

This question already has answers here: Where can I learn about PHP internals? [closed] (4 answers) Closed 1 year ago.

The community reviewed whether to reopen this question 9 months ago and left it closed:

Needs details or clarity Add details and clarify the problem by editing this post.

I know you can technically make PHP extens开发者_如何学运维ion just by making a PHP file and using require_once.

But would it optimize the performance if you wrote an extension in C or C++.

If so, how would you make a "hello-world" for that?


I know you can technically make PHP extension just by making a PHP file and using require_once.

The base of this functionality is the include statement, which includes and evaluates the specified file. Extension isn't the right term, because you are just including another PHP script file. A PHP extensions provides additional functions to the language in form of a compiled module.

But would it optimize the performance, if you wrote an extension in C or C++.

Yes, it optimizes the performance. That's why PHP extensions like CPhalcon or YAF were written.

How to make a "Hello World" PHP Extension?

I will describe how you can build a "Hello World" PHP extension in five steps.

A Debian based OS is required, because we need to fetch some tools and dependencies with apt-get.

Step 1 - Setup Build Environment / Requirements

A PHP Extension is compiled C code. We need a shell (should already be installed), an editor (your choice), a compiler (here we'll use GCC), PHP itself and PHP development dependencies for the build.

sudo apt-get install build-essential php7.0 php7.0-dev

Step 2 - Config

We need to describe our extension and the files forming it in a basic configuration file:

File: config.m4

PHP_ARG_ENABLE(php_helloworld, Whether to enable the HelloWorldPHP extension, [ --enable-helloworld-php Enable HelloWorldPHP])

if test "$PHP_HELLOWORLD" != "no"; then
    PHP_NEW_EXTENSION(php_helloworld, php_helloworld.c, $ext_shared)
fi

As you can see, the NEW_EXTENSION contains a C file: php_helloworld.c.

Step 3 - Code

Let's create the C code for our extension.

Firstly, we create a header file:

php_helloworld.h

// we define Module constants
#define PHP_HELLOWORLD_EXTNAME "php_helloworld"
#define PHP_HELLOWORLD_VERSION "0.0.1"

// then we declare the function to be exported
PHP_FUNCTION(helloworld_php);

Secondly, we create the source file:

php_helloworld.c

// include the PHP API itself
#include <php.h>
// then include the header of your extension
#include "php_helloworld.h"

// register our function to the PHP API 
// so that PHP knows, which functions are in this module
zend_function_entry helloworld_php_functions[] = {
    PHP_FE(helloworld_php, NULL)
    {NULL, NULL, NULL}
};

// some pieces of information about our module
zend_module_entry helloworld_php_module_entry = {
    STANDARD_MODULE_HEADER,
    PHP_HELLOWORLD_EXTNAME,
    helloworld_php_functions,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    PHP_HELLOWORLD_VERSION,
    STANDARD_MODULE_PROPERTIES
};

// use a macro to output additional C code, to make ext dynamically loadable
ZEND_GET_MODULE(helloworld_php)

// Finally, we implement our "Hello World" function
// this function will be made available to PHP
// and prints to PHP stdout using printf
PHP_FUNCTION(helloworld_php) {
    php_printf("Hello World! (from our extension)\n");
}

Step 4 - Build

Now, we are ready to build the extension.

First we prepare the build environment for a PHP extension:

phpize

Then we configure the build and enable our extension:

./configure --enable-php-helloworld

Finally, we can build it:

make
sudo make install

Step 5 - Test

To test our PHP extension, lets load the helloworld_php.so extension file and execute our function helloworld_php():

php -d extension=php_helloworld.so -r 'helloworld_php();'

Done :)


Building on Windows

If you try to build an Windows (YIKES!), then you need to adjust the steps a bit:

Step 2 - Config

File: config.w32

ARG_ENABLE("helloworld", "helloworld support", "yes");

if (PHP_HELLOWORLD == "yes") {
    EXTENSION("helloworld", "php_helloworld.c");
}

Step 4 - Build

Use nmake instead of make.


List of helpful resources:

  • A good book on the C programming language
  • https://wiki.php.net/internals/
  • https://wiki.php.net/internals/extensions
  • http://phpinternalsbook.com/
  • https://phpinternalsbook.com/php7/build_system/building_extensions.html
  • https://nikic.github.io/
  • http://jpauli.github.io/
  • https://github.com/php/php-src
  • http://www.slideshare.net/pierrej/extending-php-7-the-basics - explains basic argument handling
  • https://github.com/phplang/extension-tutorial - Materials for an Extension Writing Tutorial


Software written in C/C++ certainly does run faster than code in PHP. And you can write an extension in C/C++ and link it into PHP. The PHP manual covers this here: http://php.net/manual/en/internals2.php

The other answers give links to other tutorials for writing PHP extensions, and you can google for "PHP extension tutorial" to find more.

But whether this is the right thing to do in your app is another story. Most experts agree that PHP runs just fine, fast enough for 98% of applications. The instances where PHP isn't fast enough are not in general due to the language, but an inefficient application architecture that the programmer has created. That's a weakness that can't be remedied by rewriting parts of your app in C/C++.


Here's a tutorial on PHP extensions. Whether it will optimize the performance or not, it depends on what you are trying to wrap on your extension. But I would not write a PHP extension just for optimization purposes. I would write one if I have no choice. I.E. Wrapping a common C library to make it available directly in PHP...


i think (but no sure) you can do that by make an dll file and put it in ext folder which exist with php installation files.if my previous words is correct you can do that (dll) file in visual studio

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜