This article mainly introduces the method of using the lib library based on the function method in PHP7 extension development. It analyzes the encapsulation and calling related operation skills of the lib library extension in PHP7 in the form of examples. Friends in need can refer to it. I hope it can help everyone. .
Preface
First let’s talk about what a lib library is. A lib library is a file that provides specific functions. It can be regarded as a file in PHP, which provides some function methods. It's just that this lib library is written in c or c++.
Scenarios using lib library. Some software already provides lib libraries, so we don't need to implement it again. For example, the original mysql extension is encapsulated using the official mysql lib library.
In this article, we will build a simple lib library and make encapsulated calls in the extension.
Code
Basic code
For this extension, we will add # to the say extension ##call_lib_fun(). Please read this blog post for the code related to say extension. PHP7 extension development hello word This article has introduced in detail how to create an extension and provided source code download.
Code implementation
Build lib library
Add hello.h file. The code is as follows:#ifndef TEST_HEADER_FILE #define TEST_HEADER_FILE #include <stdlib.h> #include <string.h> char * show_site(); #endif
#include "hello.h" char * show_site() { char *site = malloc(15 * sizeof(char)); strcpy(site, "www.bo56.com"); return site; }
$ gcc -g -O0 -fPIC -shared -o hello.so ./hello.c
hello.so to the /usr/local/lib/ directory and name it hello.so
hello.hCopy to the /usr/local/include/ directory.
Modify the config.m4 file
Increase the extension’s dependence on the dynamic library. Mainly add the following lines of code:PHP_ADD_LIBRARY_WITH_PATH(hello, /usr/local/lib/, SAY_SHARED_LIBADD) PHP_SUBST(SAY_SHARED_LIBADD)
Write extension code
Add a reference tohello.h .
#include "php_say.h" #include <stdio.h> //下面这行是增加的 #include "hello.h"
show_site() method. The code is as follows:
PHP_FUNCTION(show_site) { char *site = show_site(); RETVAL_STRING(site); free(site); return; }
php call result
<?php $result = show_site(); var_dump($result); ?>
$php ./test.php string(12) "www.bo56.com"
Code Interpretation
PHP_ADD_LIBRARY_WITH_PATH is used to specify the name, address and other information of the lib library. The first parameter is the name and the second parameter is the address. Add the header file of the lib library to the say.c file. Use#include "hello.h".
Handling of Cookies in Python (2) cookielib library
How to use the pdflib library to read pdf files Content
Use Python’s urllib library to submit WEB forms
The above is the detailed content of Detailed explanation of how to use lib library in PHP7 based on functions. For more information, please follow other related articles on the PHP Chinese website!