Extension of PHP7 Extension Development Method of Using lib Library Based on Function

  • 2021-08-31 07:34:44
  • OfStack

This article illustrates the method of using lib library based on function developed by PHP7 extension. Share it for your reference, as follows:

Preface

First of all, let's talk about what lib library is. The lib library is a file that provides specific functionality. Think of it as a file of PHP, which provides a number of functional methods. Only this lib library is written in c or c + +.

Scenarios using the lib library. 1 Some software already provides lib library, so we don't need to repeat the implementation once. For example, the original mysql extension is encapsulated by using the official lib library of mysql.

In this article, we will build a simple lib library and make encapsulation calls in the extension.

Code

Basic code

This extension, we will add to the say extension call_lib_fun() . say extension related code please see this blog post. PHP7 Extension Development hello word has described in detail how to create an extension and provides a source download.

Code implementation

Establishing 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 the 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 generates a dynamic library file for hello. so in the current directory. The extension of dynamic libraries of different operating systems may not be the same. For example, 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
Copy hello. h to the/usr/local/include/ directory.

Modify the config. m4 file

Increase the dependence of extension on 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

Increase hello.h Reference to.


#include "php_say.h"
#include <stdio.h>
// The following line is added 
#include "hello.h"

Increase 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 results


$php ./test.php
string(12) "www.bo56.com"

Code interpretation

PHP_ADD_LIBRARY_WITH_PATH is information that specifies the name, address, and so on 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" .

Invoke methods in the lib library in the extension code just like other kernel-provided methods 1.

For more readers interested in PHP related content, please check the topics on this site: "PHP Extension Development Tutorial", "php Object-Oriented Programming Introduction Tutorial", "php+mysql Database Operation Introduction Tutorial", "PHP Network Programming Skills Summary" and "php Common Database Operation Skills Summary"

I hope this paper is helpful to everyone's PHP programming.


Related articles: