PHP扩展方法没有返回原因
php extension method not returned why?
下面是php_ex。cc。当我编译我的。so库时,return()方法不起作用。为什么?我没有错误,但它应该打印"测试",它没有。需要一些帮助。THX
[php_ex.cc]#include "php_vehicles.h"
#include "car.hpp"
zend_object_handlers car_object_handlers;
zend_object_handlers car2_object_handlers;
struct car_object {
zend_object std;
Car *car;
};
struct car2_object {
zend_object std1;
Car2 *car2;
};
zend_class_entry *car_ce;
zend_class_entry *car2_ce2;
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);
}
void car2_free_storage(void *object TSRMLS_DC)
{
car2_object *obj2 = (car2_object *)object;
delete obj2->car2;
zend_hash_destroy(obj2->std1.properties);
FREE_HASHTABLE(obj2->std1.properties);
efree(obj2);
}
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;
}
zend_object_value car2_create_handler(zend_class_entry *type TSRMLS_DC)
{
zval *tmp;
zend_object_value retval;
car2_object *obj2 = (car2_object *)emalloc(sizeof(car2_object));
memset(obj2, 0, sizeof(car2_object));
obj2->std1.ce = type;
ALLOC_HASHTABLE(obj2->std1.properties);
zend_hash_init(obj2->std1.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
zend_hash_copy(obj2->std1.properties, &type->default_properties,
(copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
retval.handle = zend_objects_store_put(obj2, NULL,
car2_free_storage, NULL TSRMLS_CC);
retval.handlers = &car2_object_handlers;
return retval;
}
PHP_METHOD(Car, __construct)
{
long maxGear;
Car *car = NULL;
zval *object = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &maxGear) == FAILURE) {
RETURN_NULL();
}
car = new Car(maxGear);
car_object *obj = (car_object *)zend_object_store_get_object(object TSRMLS_CC);
obj->car = car;
}
PHP_METHOD(Car, shift)
{
}
PHP_METHOD(Car, accelerate)
{
Car *car;
char *strr=NULL;
int strr_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &strr, &strr_len) == FAILURE) {
RETURN_NULL();
}
car_object *obj = (car_object *)zend_object_store_get_object(
getThis() TSRMLS_CC);
car = obj->car;
if (car != NULL) {
std::string s(strr);
car->accelerate(s);
RETURN_STRING(car->accelerate(s).c_str(),1);
}
}
PHP_METHOD(Car, brake)
{
}
PHP_METHOD(Car, getCurrentSpeed)
{
Car *car;
car_object *obj = (car_object *)zend_object_store_get_object(
getThis() TSRMLS_CC);
car = obj->car;
if (car != NULL) {
RETURN_LONG(car->getCurrentSpeed());
}
RETURN_NULL();
}
PHP_METHOD(Car, getCurrentGear)
{
}
PHP_METHOD(Car2, __construct)
{
Car2 *car2 = NULL;
zval *object = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "", NULL) == FAILURE) {
RETURN_NULL();
}
car2 = new Car2();
car2_object *obj = (car2_object *)zend_object_store_get_object(object TSRMLS_CC);
obj->car2 = car2;
}
PHP_METHOD(Car2, retur)
{
Car2 *car2;
car2_object *obj = (car2_object *)zend_object_store_get_object(
getThis() TSRMLS_CC);
car2 = obj->car2;
if (car2 != NULL) {
(car2->retur());
}
}
function_entry car_methods[] = {
PHP_ME(Car, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(Car, shift, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Car, accelerate, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Car, brake, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Car, getCurrentSpeed, NULL, ZEND_ACC_PUBLIC)
PHP_ME(Car, getCurrentGear, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
function_entry car2_methods[] = {
PHP_ME(Car2, retur, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
PHP_MINIT_FUNCTION(vehicles)
{
zend_class_entry ce,ce2;
INIT_CLASS_ENTRY(ce, "Car", car_methods);
INIT_CLASS_ENTRY(ce2, "Car2", car2_methods);
car_ce = zend_register_internal_class(&ce TSRMLS_CC);
car_ce->create_object = car_create_handler;
car2_ce2 = zend_register_internal_class(&ce2 TSRMLS_CC);
car2_ce2->create_object = car2_create_handler;
memcpy(&car_object_handlers,
zend_get_std_object_handlers(), sizeof(zend_object_handlers));
car_object_handlers.clone_obj = NULL;
memcpy(&car2_object_handlers,
zend_get_std_object_handlers(), sizeof(zend_object_handlers));
car2_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
[example.cc]
void Car2::retur()
{
cout<<"YOU ARE IN CLASS NR 2"<<endl;
}
我想提一下,类Car和它的方法是工作的。
如果您的retur
方法确实写入标准输出,您应该注意PHP的输出,通常不符合标准输出。
对于CLI SAPI(例如),它是这样做的,但即使这样,通过直接写入标准输出,您将绕过PHP的输出缓冲。
您必须使用php_printf
或PHPWRITE
来代替。
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- C++返回 Numpy 数组的 Python 扩展模块
- 扩展 QStandardItem 不会返回数据
- 警告 C6031 返回值在宏扩展中被忽略
- 如何将 Python 对象转换为 Cython 扩展类型的 std::vector 并返回?
- 按扩展名过滤搜索文件会返回太多结果
- 如何在代码中创建抽象类,让蓝图扩展它,并将该蓝图返回给代码进行实例化?
- 在 Python 的 C/C++ 扩展中,返回的 PyObject* 应该具有什么引用计数
- 如何在php扩展中返回$this
- 运行xvfb-run返回缺少的扩展RANDR
- 从DLL调用的C++方法返回Adobe Air本机扩展中以前调用的数据
- 我可以返回一个向量来用C++11扩展现有的向量吗
- Python C扩展:PyEval_GetLocals()返回NULL
- 是否有用于隐式返回值的GCC扩展?
- PHP扩展方法没有返回原因
- PHP扩展如何从方法返回字符串
- Python: c++扩展,返回多个值
- 通过JNI将我的Java API扩展到c++:如何包装返回String的方法