Home >Backend Development >PHP Tutorial >Detailed explanation of the method of using lib library based on function in PHP extension development

Detailed explanation of the method of using lib library based on function in PHP extension development

jacklove
jackloveOriginal
2018-06-30 17:57:521871browse

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

Add hello.c file. The code is as follows:

#include "hello.h"
char * show_site()
{
  char *site = malloc(15 * sizeof(char));
  strcpy(site, "www.bo56.com");
  return site;
}

Then use the following command to generate the lib library (dynamic library) file:

$ gcc -g -O0 -fPIC -shared -o hello.so ./hello.c

This will generate a hello.so dynamic library file in the current directory. The extensions of dynamic libraries in different operating systems may be different. For example, it is dll under windows, dylib under mac, and so under linux.

Then copy

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

hello.h reference.

#include "php_say.h"
#include <stdio.h>
//下面这行是增加的
#include "hello.h"

Add the

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);
?>

Execution 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".

In the extension code, call methods in the lib library just like calling methods provided by other kernels.

Articles you may be interested in:

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn