I know you can technically make PHP extension 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?
Use pathinfo() Function to Get File Extension in PHP We will use the built-in function pathinfo() to get the file extension. This function extracts the path information from the given path. The correct syntax to use this function is as follows. It is the string containing the path with file name and extension.
One of the biggest repositories — https://pecl.php.net/packages.php — contains almost 400 extensions. Not all of them are stable, portable among different PHP versions, and/or well supported. If you see an extension that could be useful, you need to try it to determine if it meets your requirements.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With