I’ve been writing PHP for over a year, and to be honest, the language is so easy to get started with that I don’t even want to say I know PHP (who can learn it these days). But every language has its own unique charms, and if you haven’t discovered them yet, you’re just scratching the surface of the language.
I think one of the attractions of PHP is its extensibility. If you are a c buff and PHP doesn’t have any features you want, you can write a PHP extension based on c and incorporate it into your project, which is not so nice (^_^). Of course, PHP’s performance issues have been greatly improved in PHP 7 (I’m just hearsay, 😄).
I’ll walk you through a simple PHP extension. I hope it will be helpful (I’m just afraid I’ll forget it next time). This article is based on PHP extension development and kernel application, thanks to those willing to share.
I think the great thing about the programming world is that there are so many great people out there, and it’s their open source spirit that has made the coding world so vibrant.
Let’s start with the directory structure of this extension:
- ext_mike
- config.m4
- php_mike.c
- php_mike.h
Config. m4 is used as a compilation file, and a system compilation configuration file, such as configure, is generated from the contents of config.m4 when phpize is executed.
PHP_ARG_ENABLE(mike, Whether to enable Mike extension, [ --enable-mike Enable Mike Extension])
if test "$PHP_MIKE"! ="no"; then
PHP_SUBST(MIKE_SHARED_LIBADD)
PHP_NEW_EXTENSION(mike, php_mike.c, $ext_shared)
fiCopy the code
The PHP_ARG_ENABLE function takes three arguments. The first argument is our mike extension (note that no quotes are used) and the second argument is what is displayed when we run the./configure script. The last parameter is the help we display when we call./configure –help.
The PHP_NEW_EXTENSION function declares the name of the extension, the required source file name, and the compiled form of the extension. If our extension uses more than one file, we can list the file names in the parameters of the function. The final $ext_shared parameter is used to declare that the extension is not a static module, but is loaded dynamically at PHP runtime.
Php_mike. h as a standard C language writing, define some macros, functions
#define PHP_MIKE_EXTENSION "mike"
#define PHP_MIKE_VERSION "1.0"
PHP_FUNCTION(mike);Copy the code
Php_mike. c extension module introduction, and function declaration
#include <php.h>
#include "php_mike.h"
zend_function_entry mike_functions[] = {
PHP_FE(mike, NULL)
{NULL.NULL.NULL}};Zend_module_entry mike_module_entry = {STANDARD_MODULE_HEADER, PHP_MIKE_EXTENSION, // Extension names mike_functions,NULL.NULL.NULL.NULL.NULL,
PHP_MIKE_VERSION,
STANDARD_MODULE_PROPERTIES
};
ZEND_GET_MODULE(mike)
PHP_FUNCTION(mike) {
php_printf("This is mike's first PHP extension! \n");
}Copy the code
Some of the metrics I haven’t figured out yet, because I always run it first. (This will be my next task)
How do I compile an extension
In fact, if you have installed other PHP extensions such as Redis and Mongo, then it is very easy for you to install custom PHP extensions.
Follow these steps:
phpize
./configure
make
make test
sudo make installCopy the code
Next, restart your php-fpm and use phpInfo () to see the new extension Mike.
If the extension is not found, you may need to add it manually, in php.ini
extension="mike.so"Copy the code
Next you can use the Mike Extension function in your PHP, although there is only one Mike () right now.
You can see the output:
This is where a simple PHP extension comes in. Of course, this extension doesn’t have any practical value, but it’s the key to getting you far.
Attach a useful shell command: rm – rf ls – al | grep -v – E “(. C $|. H $| config. The m4 | reco. Sh)” to delete the specified file (clip) – E – v reverse output, using regular expressions and output don’t match results