Php-cpp Development Extension (iii)

Source: Internet
Author: User

Php-cpp is a C + + library for developing PHP extensions. This section explains the implementation of PHP function parameter correlation.

specifying function parameter types

Sometimes we need to specify that the parameters of a function are arrays or specified, so can you specify the parameter type of the function in Php-cpp? The answer is yes.

Pass by value

Example:

/** * User: 公众号: 飞鸿影的博客(fhyblog) * Date: 2018/7 */#include <phpcpp.h>void example(Php::Parameters &params){}extern "C" {    PHPCPP_EXPORT void *get_module() {        static Php::Extension myExtension("my_extension", "1.0");        myExtension.add<example>("example", {            Php::ByVal("a", Php::Type::Numeric),            Php::ByVal("b", "ExampleClass"),            Php::ByVal("c", "OtherClass")        });        return myExtension;    }}

We use the Php::ByVal() specified function type, which is specified in the example Numeric and the custom class type, respectively.

Let's look at the Php::ByVal() prototypes again:

/** *  Constructor *  @param  name        Name of the parameter *  @param  type        Parameter type *  @param  required    Is this parameter required? */ByVal(const char *name, Php::Type type, bool required = true);

The first parameter, which a b c can be treated as a placeholder, is used internally and is not duplicated.
The second parameter supports the following types:

Php::Type::NullPhp::Type::NumericPhp::Type::FloatPhp::Type::BoolPhp::Type::ArrayPhp::Type::ObjectPhp::Type::StringPhp::Type::ResourcePhp::Type::ConstantPhp::Type::ConstantArrayPhp::Type::Callable

These types are actually types of variables supported by PHP.

The last parameter can be used to set whether the parameter is optional and is required by default. If you set it to true, PHP will trigger an error if the function is called without this parameter.

Let's take the sum_n function as an example:

extension.add<sum_n>("sum_n", {    Php::ByVal("a", Php::Type::Numeric, true)});

If you do not give parameters when using, PHP Warning:

PHP Warning:  sum_n() expects at least 1 parameter(s), 0 given in /media/d/work/php-ext/phpcpp/phpcpp_helloworld/test.php on line 4

Php::ByVal()There is also a prototype:

/** *  Constructor *  @param  name        Name of the parameter *  @param  classname   Name of the class *  @param  nullable    Can it be null? *  @param  required    Is this parameter required? */ByVal(const char *name, const char *classname, bool nullable = false, bool required = true);

More nullable : Whether it can be used NULL instead of parameters. Like what:

extension.add<say_class>("say_class", {    Php::ByVal("class_name", "Datetime", true, true)});

say_classin this method, we specify the parameter as a Datetime type, and you can use NULL instead, which is required. If you nullable change to False, you must pass the specified type Datetime .

Reference delivery

Sometimes we need to support the function to directly modify the original value of the variable, you need to use the reference method to pass the parameter. The Php-cpp also provides Php::ByRef support.

/** *  Constructor *  @param  name        Name of the parameter *  @param  type        Parameter type *  @param  required    Is this parameter required? */ByRef(const char *name, Php::Type type, bool required = true);

Example:

/** * User: 公众号: 飞鸿影的博客(fhyblog) * Date: 2018/7 */#include <phpcpp.h>void swap(Php::Parameters &params){    Php::Value temp = params[0];    params[0] = params[1];    params[1] = temp;}extern "C" {    PHPCPP_EXPORT void *get_module() {        static Php::Extension myExtension("my_extension", "1.0");        myExtension.add<swap>("swap", {            Php::ByRef("a", Php::Type::Numeric),            Php::ByRef("b", Php::Type::Numeric)        });        return myExtension;    }}

We use test.php to test:

<?php// define two variables$a = 1;$b = 2;// 交换变量swap($a, $b);// 下面使用错误,仅支持变量引用//swap(10,20); //会触发PHP Fatal error:  Only variables can be passed by referencevar_dump($a, $b);?>

(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 (iii)

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.