This article is to share the content of PHP extension development of the development of a our own mathematical function library, has a certain reference value, the need for friends can refer to
PHP Extension is one of the skills that advanced PHP programmers must understand, how can a beginner PHP extension developer develop a mature extension into the advanced areas of PHP development? This series of development tutorials will take you from entry to the advanced stage.
This tutorial series was developed under Linux (recommended for CentOS), PHP is 5.6, and assumes you have a certain Linux experience and a C/s base.
There are problems need to communicate with friends Please add QQ Technology Group 32550793 and I communicate.
The previous chapter demonstrates a hello world extension, with a basic understanding of the basic style of the extended C + + source code developed with Php-cpp. Next, develop a simple math database (MyMath) to familiarize yourself with how to export various interface functions.
The code for the MyMath Math library is already on GitHub and can be downloaded directly from Git or the Web browser to open the source.
git download command line
git clone https://github.com/elvisszhang/phpcpp_mymath.git
Browser download URL and warehouse URL: Https://github.com/elvisszhan ...
One, without parameters, no return value of the extension function notation
function function: Print a prime number within 100
Function Name: mm_print_pn_100
How to register an extension function
You must register the function mm_print_pn_100 in the Get_module function body so that it can be called directly in PHP.
Phpcpp_export void *get_module () { ///Must be a static type because the extension object needs to reside in the PHP process in memory static php::extension Extension (" MyMath "," 1.0.0 "); Here you can add the function extension.add<mm_print_pn_100> ("mm_print_pn_100") you want to expose to PHP calls. Returns the extension object pointer return extension;}
The function declaration and Code are as follows.
The function does not require arguments, the function's argument list does not have to put anything, empty is OK. The function does not need a return value and the return value type is set to void.
Prints a prime number within 100 void mm_print_pn_100 () { int x = 2; int y = 1; int line = 0; while (x <=) { int z = x-y;//z decrements by 1 int a = x%z;//Take remainder if (a = = 0) {//= if x is divisible by z if (z = = 1) {//if Z For 1 (x is prime) php::out << x << "";//output x line ++;//the number of outputs per row plus 1 } x + +; X plus 1 y = 1;//y restore } else {//if it is not divisible by y ++;//y plus 1, the next loop is Z minus 1 } if (line = = 10) {//Outputs 10 numbers per output php::out << std::endl;//output a newline line = 0;//restore lines } if (line ! = 0)//last line output wrap Php::out << Std::endl; Php::out.flush ();}
PHP Test Code
<?php//print a prime number within 100 mm_print_pn_100 ();
Run the above PHP code, the output is
2 3 5 7 11 13 17 19 23 2931 37 41 43 47 53 59 61 67 7173 79 83 89 97
Two, without parameters, with the return value of the extension function notation
function function: Calculates 1, 2, 3 、...、 100 and
Function Name: mm_sum_1_100
Registration function mm_sum_1_100, registered in the same section
Extension.add<mm_sum_1_100> ("mm_sum_1_100");
The function declaration and Code are as follows.
The function does not require arguments, and the function argument list is set to NULL.
The function has a return value and the return value type is set to Php::value. Because Php::value overloads the constructors and operator = operators, common data types (shaping, strings, floating-point numbers, arrays, and so on) can be returned directly.
Gets 1-100 and Php::value mm_sum_1_100 () { int sum = 0; int i; for (i=1;i<=100;i++) { sum + = i; } return sum; You can return the sum value directly and generate the Php::value type automatically}
PHP Test Code:
<?php$sum = mm_sum_1_100 (); Echo ' sum (1~100) = '. $sum. Php_eol;? >
Run the above PHP code, the output is
SUM (1~100) = 5050
Three, with parameters, no return value of the extension function notation
function function: Computes any given integer and prints all primes within that integer
Function Name: Mm_print_pn_any
Registration function Mm_print_pn_any, registered in the same section
Extension.add<mm_print_pn_any> ("Mm_print_pn_any");
The function declaration and Code are as follows. Due to the need for parameters, function parameters need to be written in PHP::P arameters ¶ms, because there is no return value, the return value type is set void.
Also need to detect whether the parameters are input, the type of parameters need to detect is not shaping. The code is prone to exception if it is not detected directly.
Any given integer that prints out all prime numbers less than or equal to the integer void Mm_print_pn_any (Php::P arameters ¶ms) {//check must enter a parameter if (params.size () = = 0) { Php::out << "error:need a parameter" << Std::endl; Return }//Check parameters must be shaped if (params[0].type ()! = php::type::numeric) {php::out << "Error:parameter must be numeri C "<< Std::endl; Return }//check digit must be greater than 1 int number = Params[0]; if (number <= 1) {php::out << "Error:parameter must be larger than 1" << Std::endl; Return }//Check parameters must be greater than 0 int x = 2; int y = 1; int line = 0; while (x <= number) {int z = x-y;//z decrements with y 1 int a = X%Z;//Take remainder if (a = = 0) {//If x is divisible by Z if (z = = 1) {//if z is 1 (x is prime) php::out << x << "";//output X line ++;//number of outputs per row plus 1 } x + +; X plus 1 y = 1;//y Restore} else {//if it is not divisible by y ++;//y plus 1, the next loop is Z minus 1} if (line == 10) {//output 10 number php::out << std::endl;//output a newline line = 0;//Restore lines} if (line! = 0)//last line output newline php::out << Std::endl; Php::out.flush (); }
PHP Test Code
<?phpecho '---runing mm_print_pn_any ()---'. Php_eol;mm_print_pn_any (); Echo Php_eol. '---runing mm_print_pn_any (\ ' xyz\ ')---'. Php_eol;mm_print_pn_any (' xyz '); Echo Php_eol. '---runing mm_print_pn_any---'. Php_eol;mm_print_pn_any (200);? >
Run the above PHP code, the output is
---runing mm_print_pn_any ()---error:need a parameter---runing mm_print_pn_any (' xyz ')---error:parameter must be Numeric---runing mm_print_pn_any ($)---2 3 5 7 11 13 17 19 23 2931 37 41 43 47 53 59 61 67 7173 79 83 89 97 101 103 107 1 09 113127 131 137 139 149 151 157 163 167 173179 181 191 193 197 199
Four, scalar type parameters, with the return value of the extension function notation
function function: Calculates the sum of a series of parameters given
Function Name: Mm_sum_all
Register extension function Mm_sum_all, register in the same section
Extension.add<mm_sum_all> ("Mm_sum_all");
The function declaration and Code are as follows.
Get all parameters and Php::value Mm_sum_all (Php::P arameters ¶ms) { int sum = 0; The for (auto ¶m:params) { //String type can be automatically converted into an integer + = param; } return sum;}
PHP Test Code
<?php$sum = Mm_sum_all (1, 2, ' 3 ', ' 5 '); The string type can be automatically converted into a reshape echo ' Sum (1,2,\ ' 3\ ', \ ' 5\ ') = '. $sum. Php_eol;? >
Test output results:
SUM (1, 2, ' 3 ', ' 5 ') = 11
Five, array-type parameters, with the return value of the extension function notation
function function: Calculates the sum of all elements of an array, given an array type parameter
Function Name: Mm_sum_array
Registration function Mm_sum_array, registered in the same way as the first section
The function declaration and Code are as follows.
Gets all the elements of the array and Php::value Mm_sum_array (Php::P arameters ¶ms) { //does not have a given parameter, returns 0 if (params.size () = = 0) { return 0; } The parameter type is not an array, and the turn into shaping returns if (Params[0].type ()! = Php::type::array) { return (int) params[0]; } The elements in the array are added individually by int sum = 0; Php::value array = params[0]; int size = Array.size (); int i; for (i=0;i<size;i++) { sum + = Array.get (i); } return sum;}
PHP Test Code
<?php$nums = Array (1,3,5,7), $sum = Mm_sum_array ($nums); Echo ' Sum (Array (1,3,5,7) = '. $sum. Php_eol;? >
Test output results:
Sum (Array (1,3,5,7)) = 16
Six, the return value type is an array of extension function notation
The return value of the above function is a scalar type, the array is a particularly common type of PHP, if you want to return an array type, you can use C + + Std::vector,php-cpp will be intimate to automatically convert it to PHP-aware array type.
Our present function is to "return an array of all primes within 30". Extend the inside register function in the same way as the first section.
The function declaration and Code are as follows.
Get all primes within 30 php::value mm_get_pn_30 () { std::vector<int> pn; int x = 2; int y = 1; while (x <=) { int z = x-y;//z decrements by 1 int a = x%z;//Take remainder if (a = = 0) {//= if x is divisible by z if (z = = 1) {//if z is 1 (x is prime number) Pn.push_back (x);//drop array in } x + +,//x plus 1 y = 1;//y restore } else {//If not divisible by y ++;// Y plus 1, Next loop z minus 1 } } return pn;
PHP Test Code
<?PHP$PN = Mm_get_pn_30 (); Var_dump ($PN);? >
Test output results:
Array (Ten) { [0]=> int (2) [1]=> Int (3) [2]=> Int (5) [3]=> Int (7) [4]=> Int (one) [5]=> int ( [6]=> int) [7]=> int (+) [8]=> Int ([9]=>) Int (29)}
Vii. Reference Documents
C + + prime number determination and output quality tables
Php-cpp function development help