Php-cpp Development Extension (ii)

Source: Internet
Author: User
Tags deprecated php language php error php script

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::outA 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::flushDisplay refresh buffer;
    • std::endlOutput line break and refresh buffer;
    • Php::outOutput content to buffer;
    • Php::noticeGenerate a PHP notice;
    • Php::warningGenerate a PHP warning;
    • Php::deprecatedGenerate a PHP deprecated;
    • Php::errorGenerate 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::errorflow 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::ValueCan 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 &params){}

4, the tangible parameter has the return value

Php::Value func(Php::Parameters &params){}

Example:

/** * User: 公众号: 飞鸿影的博客(fhyblog) * Date: 2018/7 */Php::Value sum_n(Php::Parameters &params){    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)

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.