Php-cpp is a C + + library for developing PHP extensions. This section explains the implementation of PHP output and functions.
Output and Errors
In the HelloWorld example above, we used the Php::out
output and used a std::endl
newline refresh buffer. Php::out
A variable is actually std::ostream
an instance of the class that supports all the output buffers set in PHP. It is basically the same as a function in a PHP script echo()
.
Several common ways to explain:
std::flush
Display refresh buffer;
std::endl
Output line break and refresh buffer;
Php::out
Output content to buffer;
Php::notice
Generate a PHP notice;
Php::warning
Generate a PHP warning;
Php::deprecated
Generate a PHP deprecated;
Php::error
Generate a PHP error
For errors, notifications, and warnings, we don't need a newline character, but we still have to flush the buffer to actually generate the output. Php::error
flow has something very special: when you refresh it, the PHP script ends with a fatal error.
With some of the above methods, we can implement the standard output function of PHP.
The use of output must introduce a iostream
header file.
Implementing PHP Functions
In the above example, we've actually implemented 2 PHP functions. Next, let's look at how Php-cpp implements the following 4 kinds of functions:
- Invisible parameter no return value
- The invisible parameter has a return value
- No return value for physical parameters
- Tangible parameters have return values
Thanks to the php-cpp packages Php::Value
and Php::Parameters
classes, we can implement these functions very simply.
Php::Value
Can be thought of as PHP variables, the internal encapsulation of the ZVAL structure. By overloading the operators, variables in C + + support automatic conversion to Php::Value
type.
1. No return value for invisible parameter
void func(){}
2, the invisible parameter has the return value
Php::Value func(){}
3. No return value for tangible parameters
void func(Php::Parameters ¶ms){}
4, the tangible parameter has the return value
Php::Value func(Php::Parameters ¶ms){}
Example:
/** * User: 公众号: 飞鸿影的博客(fhyblog) * Date: 2018/7 */Php::Value sum_n(Php::Parameters ¶ms){ int max = 0, sum = 0; if(params.size() == 0){ Php::warning << "miss param" << std::flush; return 0; } max = params[0]; if(params[0].type() != Php::Type::Numeric){ Php::warning << "param type must be numeric." << std::flush; return 0; } for(int i = 1; i <= max; i++){ sum += i; } return sum;}
The function realizes the return value of the tangible parameter, and several other modifications can be realized. Although the function returns a C + + int type variable, it is Php::Value
automatically converted to the PHP language type variable.
We only need to get_module()
register in:
extension.add<sum_n>("sum_n");
Tips: The final exposed function name can be different from the C + + functor name, for example:
extension.add<sum_n>("sum_n2");
is also possible.
To recompile, you can:
$ sudo make clean && make && sudo make install
(not to be continued)
Want to be the first time to get the latest news, welcome to pay attention 飞鸿影的博客(fhyblog)
to, not regularly for you to present technical dry.
Php-cpp Development Extension (ii)