c++ php 扩展中的类
我想问一下,在创建扩展时,_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
I would like to ask if it's importat the name for _object_handlers, php_minit_function when creating an extension:
I have the following:
[test.h]
public:
class A
{
public:
A();a
class B{
public:
B();
class Dat
{
//variables
};
//methods
bool first_method(A::B::dat &d);
};
};
I need some help when creating the 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须使用扩展函数的 C 调用约定。因此,您应该将它们包装在 extern "C" {} 声明中,以便 PHP 可以调用它们。
另请注意,PHP_METHOD(foo, bar) 变为 zim_foo_bar,因此您的 PHP_METHOD(A::b, set) 变为 zim_A::b_set,这解释了您的错误消息。
you have to use C calling conventions for extension functions. So you should wrap them in a extern "C" {} declaration so PHP can call them.
Also mind that PHP_METHOD(foo, bar) becomes zim_foo_bar, so your PHP_METHOD(A::b, set) becomes zim_A::b_set, which explains your error message.