


Detailed explanation of the method of using lib library based on function in PHP extension development
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
The example of this article describes the method of using lib library based on function in PHP7 extension development. Share it with everyone for your reference, the details are as follows:
Preface
First of all, 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
Addhello.h reference.
#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".
Think about the particularity of the volist tag in php in ajax operations
Detailed explanation of the volist tag in thinkphp
Explanation of knowledge related to PHP implementation of finding the entry node of the ring in the linked list
The above is the detailed content of Detailed explanation of the method of using lib library based on function in PHP extension development. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To resolve the plugin not showing installed issue in PHP 7.0: Check the plugin configuration and enable the plugin. Restart PHP to apply configuration changes. Check the plugin file permissions to make sure they are correct. Install missing dependencies to ensure the plugin functions properly. If all other steps fail, rebuild PHP. Other possible causes include incompatible plugin versions, loading the wrong version, or PHP configuration issues.

How to install the mongo extension in php7.0: 1. Create the mongodb user group and user; 2. Download the mongodb source code package and place the source code package in the "/usr/local/src/" directory; 3. Enter "src/" directory; 4. Unzip the source code package; 5. Create the mongodb file directory; 6. Copy the files to the "mongodb/" directory; 7. Create the mongodb configuration file and modify the configuration.

In php5, we can use the fsockopen() function to detect the TCP port. This function can be used to open a network connection and perform some network communication. But in php7, the fsockopen() function may encounter some problems, such as being unable to open the port, unable to connect to the server, etc. In order to solve this problem, we can use the socket_create() function and socket_connect() function to detect the TCP port.

Common solutions for PHP server environments include ensuring that the correct PHP version is installed and that relevant files have been copied to the module directory. Disable SELinux temporarily or permanently. Check and configure PHP.ini to ensure that necessary extensions have been added and set up correctly. Start or restart the PHP-FPM service. Check the DNS settings for resolution issues.

How to install and deploy php7.0: 1. Go to the PHP official website to download the installation version corresponding to the local system; 2. Extract the downloaded zip file to the specified directory; 3. Open the command line window and go to the "E:\php7" directory Just run the "php -v" command.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

PHP extension development is the art of creating custom functionality, extending PHP core functionality and building more powerful applications. It opens up new possibilities in the PHP world, allowing developers to transcend the basic limitations of the language. This article will take you on a journey of PHP extension development, providing you with comprehensive knowledge and practical guidance from basic concepts to advanced techniques. PHP extension development basics Before starting PHP extension development, you need to understand some basic concepts. What are PHP extensions? A PHP extension is a dynamic link library (DLL) that extends PHP core functionality and provides new data types, functions and classes. Advantages of PHP Extensions PHP extensions have many advantages, including: scalability, flexibility, performance optimization, and code reuse. PHP
