如何使用PHPCPP传递对象数组,遍历每个对象并返回关联数组

时间:2022-05-02 16:32:23

I have recently started learning PHPCPP - A C++ library for developing PHP extensions and trying to understand:

我最近开始学习PHPCPP - 一个用于开发PHP扩展并尝试理解的C ++库:

  1. how to pass an Array of objects from php to C++ through PHPCPP library as examples give only info about arrays and objects separately,
  2. 如何通过PHPCPP库将一个对象数组从PHP传递到C ++,例如只给出有关数组和对象的信息,

  3. then how to loop through each object in C++
  4. 那么如何在C ++中遍历每个对象

  5. and how to return an associative array back to PHP
  6. 以及如何将关联数组返回给PHP

Can someone point me to the right direction?

有人能指出我正确的方向吗?

I have come up with this example however need some help:

我想出了这个例子,但需要一些帮助:

class Curve{
 public :
    double shape;
    double peak;
    double tpeak;
    double start;
    double lag;
    double index;
};

Php::Value example1(Php::Parameters &params) { 
    vector<Curve> c = params[0];

    //create an associative array and return to php
    std::map< std::string, std::map<std::string, std::map<std::string, std::map<std::string, std::double>>> > data;
    // loop through array of objects here or do something with an array
    ...
    data[c.shape][c.peak][c.tpeak][c.start] = 1/12 * c.index;

    return data;
}


extern "C" {
    PHPCPP_EXPORT void *get_module() {
        static Php::Extension myExtension("my_extension", "1.0");
        myExtension.add<example1>("example1", { 
                Php::ByVal("curves", "Array", false); 
        });
        eturn myExtension;
    }
}

1 个解决方案

#1


0  

In contrary to your declaration Php::ByVal("curves", "Array", false); I am using Php::ByVal("curves", "Array", true);

与您的声明相反Php :: ByVal(“curves”,“Array”,false);我正在使用Php :: ByVal(“curves”,“Array”,true);

I did not test this piece of code, therefore it may be necessary to add some slight corrections :-)

我没有测试这段代码,因此可能需要添加一些细微的修正:-)

Good luck!

public:

  Php::Value GetAssociativeArray( Php::Parameters &params ) {

        if ( params.size( ) < 1 ) {
            error_function( "need at least 1 parameter" );        
        }

        if ( ! params[ 0 ].isObject( ) ) {
            error_function( "needs an object as first parameter" );
        }


        //
        // what you have to do before you can use this example:
        // define the data type data_t, which should be stored in php_array_result
        // supply the PHP class the objects in php_array_objects belong to -  can be coded in PHP or cpp
        // supply the PHP class the objects in php_array_result belong to -  can be coded in PHP or cpp
        //

        // the PHP object associated to the current cpp class
        Php::Value self( this );
        // the objects received
        Php::Value php_array_objects  = params[ 0 ];
        // the PHP array to return
        Php::Array php_array_result;
        // the c++ - class
        Curve * obj_curve;
        // a single PHP object from the PHP array we received
        Php::Value php_obj_in
        // a single object from the PHP array we are delivering
        Php::Object php_obj_out;
        // the key of the associative PHP Array
        std::string array_key;
        // the data to collect in the associative PHP array
        data_t data;        
        // some other data
        int64_t data_returned;


        for ( int i = 0; i < php_array_objects.size( ) ; i++ ) {

            // retrieve the next object
            php_obj_in = php_array_objects[ i ];

            // cast PHP object to c++-class
            obj_curve = ( Curve * ) php_obj_in.implementation( );

            // do something with obj_curve

            data = obj_curve->do_something( );

            // calculate the key

            key = "key_" + std::to_string( i );

            // to create an object pass in the class name ( Curve ) to the constructor with optional constructor parameters (1,2,3)

            php_obj_out = Php::Object( "Curve", 1, 2, 3 );

            // set a class member named "class_member" of php_obj_out

            php_obj_out[ "class_member" ] = data;

            // call the method "class_method" of php_obj_out

            data_returned = php_obj_out.call( "class_method", "parameter" );

            // add the new  created object to the PHP array

            php_array_result[ key ] = php_obj_out;

        }

        return php_array_result;

    }   // GetAssociativeArray( )    

#1


0  

In contrary to your declaration Php::ByVal("curves", "Array", false); I am using Php::ByVal("curves", "Array", true);

与您的声明相反Php :: ByVal(“curves”,“Array”,false);我正在使用Php :: ByVal(“curves”,“Array”,true);

I did not test this piece of code, therefore it may be necessary to add some slight corrections :-)

我没有测试这段代码,因此可能需要添加一些细微的修正:-)

Good luck!

public:

  Php::Value GetAssociativeArray( Php::Parameters &params ) {

        if ( params.size( ) < 1 ) {
            error_function( "need at least 1 parameter" );        
        }

        if ( ! params[ 0 ].isObject( ) ) {
            error_function( "needs an object as first parameter" );
        }


        //
        // what you have to do before you can use this example:
        // define the data type data_t, which should be stored in php_array_result
        // supply the PHP class the objects in php_array_objects belong to -  can be coded in PHP or cpp
        // supply the PHP class the objects in php_array_result belong to -  can be coded in PHP or cpp
        //

        // the PHP object associated to the current cpp class
        Php::Value self( this );
        // the objects received
        Php::Value php_array_objects  = params[ 0 ];
        // the PHP array to return
        Php::Array php_array_result;
        // the c++ - class
        Curve * obj_curve;
        // a single PHP object from the PHP array we received
        Php::Value php_obj_in
        // a single object from the PHP array we are delivering
        Php::Object php_obj_out;
        // the key of the associative PHP Array
        std::string array_key;
        // the data to collect in the associative PHP array
        data_t data;        
        // some other data
        int64_t data_returned;


        for ( int i = 0; i < php_array_objects.size( ) ; i++ ) {

            // retrieve the next object
            php_obj_in = php_array_objects[ i ];

            // cast PHP object to c++-class
            obj_curve = ( Curve * ) php_obj_in.implementation( );

            // do something with obj_curve

            data = obj_curve->do_something( );

            // calculate the key

            key = "key_" + std::to_string( i );

            // to create an object pass in the class name ( Curve ) to the constructor with optional constructor parameters (1,2,3)

            php_obj_out = Php::Object( "Curve", 1, 2, 3 );

            // set a class member named "class_member" of php_obj_out

            php_obj_out[ "class_member" ] = data;

            // call the method "class_method" of php_obj_out

            data_returned = php_obj_out.call( "class_method", "parameter" );

            // add the new  created object to the PHP array

            php_array_result[ key ] = php_obj_out;

        }

        return php_array_result;

    }   // GetAssociativeArray( )