PHP扩展中的c++类

c++ classes inside php extension

本文关键字:c++ 扩展 PHP      更新时间:2023-10-16

我想问一下,在创建扩展时,_object_handlers, php_minit_function的名称是否重要:我有以下内容:

[test.h]

public:
class A
{
public:
A();a
class B{
public:
B();
class Dat
{
//variables
};
//methods
bool first_method(A::B::dat &d);
};
};

我需要一些帮助,当创建php_header,

php_extenison, config.m4

I did this (i have errors: zim_A not found)
php_A.h
#ifndef PHP_VEHICLES_H
#define PHP_VEHICLES_H
#define PHP_VEHICLES_EXTNAME  "vehicles"
#define PHP_VEHICLES_EXTVER   "0.1"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif 
#ifdef __cplusplus
extern "C" {
    #endif
#ifdef ZTS
#include "TSRM.h"
#endif
#ifdef __cplusplus
}
#endif
#ifdef __cplusplus
extern "C" {
    #endif
#include "php.h"
#ifdef __cplusplus
}
#endif

extern zend_module_entry vehicles_module_entry;
#define phpext_vehicles_ptr &vehicles_module_entry;
#endif /* PHP_VEHICLES_H */
php_tst.cc
{
#include "php_vehicles.h"
#include "MessageSphereSDK.hpp"

zend_object_handlers car_object_handlers;
struct car_object {
    zend_object std;
    A::B *car;
};
zend_class_entry *car_ce;
void car_free_storage(void *object TSRMLS_DC)
{
    car_object *obj = (car_object *)object;
    delete obj->car; 
    zend_hash_destroy(obj->std.properties);
    FREE_HASHTABLE(obj->std.properties);
    efree(obj);
}
zend_object_value car_create_handler(zend_class_entry *type TSRMLS_DC)
{
    zval *tmp;
    zend_object_value retval;
    car_object *obj = (car_object *)emalloc(sizeof(car_object));
    memset(obj, 0, sizeof(car_object));
    obj->std.ce = type;
    ALLOC_HASHTABLE(obj->std.properties);
    zend_hash_init(obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
    zend_hash_copy(obj->std.properties, &type->default_properties,
        (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
    retval.handle = zend_objects_store_put(obj, NULL,
        car_free_storage, NULL TSRMLS_CC);
    retval.handlers = &car_object_handlers;
    return retval;
}

PHP_METHOD(A::B __construct)
{
    long maxGear;
    A::B *car = NULL;
    zval *object = getThis();
    car = new A::B();
    car_object *obj = (car_object *)zend_object_store_get_object(object TSRMLS_CC);
    obj->car = car;
}
PHP_METHOD(A::B, set)
{
    A::B*car;
    car_object *obj = (car_object *)zend_object_store_get_object(
        getThis() TSRMLS_CC);
    car = obj->car;
    if (car != NULL) {
        Services::Access::Data variab;
        car->Set(&$variab);
    }
}

function_entry car_methods[] = {
    PHP_ME(A::b,  __construct,     NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
    PHP_ME(A:::B::dat,  set,           NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};
PHP_MINIT_FUNCTION(vehicles)
{
   zend_class_entry ce;
    INIT_CLASS_ENTRY(ce, "A", car_methods);
    car_ce = zend_register_internal_class(&ce TSRMLS_CC);
    car_ce->create_object = car_create_handler;
    memcpy(&car_object_handlers,
        zend_get_std_object_handlers(), sizeof(zend_object_handlers));
    car_object_handlers.clone_obj = NULL;
    return SUCCESS;
}
zend_module_entry vehicles_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
    STANDARD_MODULE_HEADER,
#endif
    PHP_VEHICLES_EXTNAME,
    NULL,        /* Functions */
    PHP_MINIT(vehicles),        /* MINIT */
    NULL,        /* MSHUTDOWN */
    NULL,        /* RINIT */
    NULL,        /* RSHUTDOWN */
    NULL,        /* MINFO */
#if ZEND_MODULE_API_NO >= 20010901
    PHP_VEHICLES_EXTVER,
#endif
    STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_VEHICLES
extern "C" {
ZEND_GET_MODULE(vehicles)
}
#endif

}
I am compiling with phpize...and so on.

config.m4

PHP_ARG_ENABLE(vehicles,
    [Whether to enable the "vehicles" extension],
    [  --enable-vehicles      Enable "vehicles" extension support])
if test $PHP_VEHICLES != "no"; then
    PHP_REQUIRE_CXX()
    PHP_SUBST(VEHICLES_SHARED_LIBADD)
    PHP_ADD_LIBRARY(stdc++, 1, VEHICLES_SHARED_LIBADD)
    PHP_NEW_EXTENSION(vehicles, vehicles.cc test.cc, $ext_shared)
fi

必须使用C调用约定来调用扩展函数。所以你应该把它们包装在extern "C"{}声明中,这样PHP就可以调用它们了。

还要注意PHP_METHOD(foo, bar)变成zim_foo_bar,所以你的PHP_METHOD(A::b, set)变成zim_A::b_set,这解释了你的错误信息