PHP7 using LIB library case code analysis based on function mode

Source: Internet
Author: User
This time, we bring you PHP7. Using LIB library case code analysis based on function mode, PHP7 the considerations of using Lib Library based on function mode, here is the actual case, take a look.

Objective

First, what is Lib library. A Lib library is a file that provides specific functionality. You can think of it as a PHP file that provides some function methods. But this Lib library is written in C or C + +.

Use the Lib library scenario. Some software has already provided Lib library, we do not need to repeat implementation once. For example, the original MySQL extension is the encapsulation using the official MySQL Lib library.

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

Code

Base Code

This extension, we will add on the say extension call_lib_fun() . Say extension related code Everyone please read this blog post. PHP7 extended development of Hello Word has detailed how to create an extension and provide a source download.

Code implementation

Build Lib Library

Add the 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 (* * 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. Different operating system dynamic libraries may not have the same extension. such as under Windows is DLL,MAC under is Dylib,linux is so.

Then copy the hello.so to the /usr/local/lib/ directory, named hello.so to copy hello.h to /usr/local/ The include/ directory.

Modify the Config.m4 file

Increase the dependency of the extension on the dynamic library. The following lines of code are mainly added:

Php_add_library_with_path (Hello,/usr/local/lib/, Say_shared_libadd) Php_subst (Say_shared_libadd)

Writing extension Code

hello.hthe added reference.

#include "php_say.h" #include <stdio.h>//the following line is an added # include "Hello.h"

Add show_site() method. The code is as follows:

Php_function (show_site) {  char *site = Show_site ();  Retval_string (site);  Free (site);  return;}

PHP Call Results

<?php$result = Show_site (); Var_dump ($result);? >

Execution results

$php./test.phpstring () "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 argument is the address.

Add the Lib Library header file to the Say.c file. Use #include "hello.h" .

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP get binary tree mirroring steps

PHP gets a list of links from the back-to-front number of nodes steps

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.