Home >Backend Development >PHP Tutorial >PHP7 uses lib library case code analysis based on function method

PHP7 uses lib library case code analysis based on function method

php中世界最好的语言
php中世界最好的语言Original
2018-05-19 15:01:571547browse

This time I will bring you a case code analysis of PHP7 using the lib library based on the function method. What are the precautions for PHP7 using the lib library based on the function method. The following is a practical case, let's take a look.

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 mysql official 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
In this way, a hello.so dynamic library file will be generated 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 the

reference of hello.h.

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

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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps for PHP to obtain the binary tree image

The steps for PHP to obtain the nodes of the linked list from back to front Detailed explanation

The above is the detailed content of PHP7 uses lib library case code analysis based on function method. 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