In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to call the FFI extension in PHP. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
What is FFI?
FFI, Foreign Function Interface, external function interface. This extension allows us to load some common libraries (.dll, .so), which means that we can call some C data structures and functions. It is already an extension released with the PHP source code, and it can be compiled directly into PHP programs by adding-with-ffi at compile time.
We already have a compiled PHP here, so we can find the extension directly and install it with a simple extension installation step.
Cd php-7.4.4/ext/ffi/
Phpize
. / configure
Make & & make install
Remember to open the extension in the php.ini file after the installation is complete. One thing to note about this extension is that it has a configuration item called ffi.enable, which by default has a value of "preload", which enables FFI only in the CLI SAPI environment. Of course, we can also change it to "true" or "false" to turn it on and off. Setting it to "true" will enable this extension in any environment.
Call the function of C using FFI
Next, take a brief look at how it calls C's functions.
/ / create a FFI object, load libc and import the printf function
$ffi_printf = FFI::cdef (
"int printf (const char * format,...);", / / definition rules of C
"libc.so.6"); / / specify the libc library
/ / call the printf function of C
$ffi_printf- > printf ("Hello% s!\ n", "world"); / / Hello World
/ / load math and import pow function
$ffi_pow = FFI::cdef (
"double pow (double x, double y);"
"libboost_math_c99.so.1.66.0")
/ / the pow function of C is called here, not PHP's own
Echo $ffi_pow- > pow (2mam 3), PHP_EOL; / / 8
We created two objects and called the printf () and pow () functions of C, respectively. FFI::cdef () is used to create a FFI object that takes two parameters, one of which is a string containing a sequence of declarations in the regular C language (types, structures, functions, variables, and so on). In fact, this string can be copied and pasted from the C header file. The other parameter is the name of the shared library file to load and define the link. That is, the .dll or .so file we need, which corresponds to our declaration string. For example, there is no calculation function such as pow () in libc.so.6, so we need to find the C language calculation function library related to math.
Define variables and arrays
Of course, FFI can also define variables and arrays.
/ / create an int variable
$x = FFI::new ("int")
Var_dump ($x-> cdata); / / int (0)
/ / assign values to variables
$x-> cdata = 5
Var_dump ($x-> cdata); / / int (5)
/ / calculate variables
$x-> cdata + = 2
Var_dump ($x-> cdata); / / int (7)
/ / combine the above two FFI object operations
Echo "pow value:", $ffi_pow- > pow ($x-> cdata, 3), PHP_EOL
/ / pow value:343
$ffi_printf- > printf ("Int Pow value is:% f\ n", $ffi_pow- > pow ($x-> cdata, 3))
/ / Int Pow value is: 343.000000
/ / create an array
A = FFI::new ("long [1024]")
/ / assign values to the array
For ($I = 0; $I < count ($a); $iTunes +) {
$a [$I] = $I
}
Var_dump ($a [25]); / / int (25)
$sum = 0
Foreach ($an as $n) {
$sum + = $n
}
Var_dump ($sum); / / int (523776)
Var_dump (count ($a)); / / int (1024) array length
Var_dump (FFI::sizeof ($a)); / / int (8192), memory size
Use the FFI::new () function to create a C data structure, the variable declaration, whose contents will be stored in the cdata property. The array can directly manipulate the return value of this function. Of course, when we want to end the use, we still need to use FFI::free () to release variables, just like the development of C language.
On how to call the FFI extension in PHP to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.