如何在php扩展中引用php类!
这是我的代码:
zval *obj;
MAKE_STD_ZVAL(obj);
zend_class_entry *foo_ce;
if( zend_hash_find( EG(class_table), "foo", sizeof("foo"), (void **)&foo_ce ) == SUCCESS ){
printf("get foo success\n");
object_init_ex(obj, foo_ce);
} else {
printf("get class failed\n");
}
printf("here\n");
但当它转到
Segmentation fault
片段时,我得到了object_init_ex
!gdb信息如下:
\#0 0x0000000000925038 in zend_hash_apply_deleter (ht=0xfac750, p=0x120b980) at /root/php-5.5.38/Zend/zend_hash.c:619
\#1 0x00000000009257b2 in zend_hash_reverse_apply (ht=0xfac750, apply_func=0x8fe67f <clean_non_persistent_class>) at /root/php-5.5.38/Zend/zend_hash.c:804
\#2 0x00000000008fec25 in shutdown_executor () at /root/php-5.5.38/Zend/zend_execute_API.c:300
\#3 0x000000000091297d in zend_deactivate () at /root/php-5.5.38/Zend/zend.c:946
\#4 0x000000000088c26f in php_request_shutdown (dummy=0x0) at /root/php-5.5.38/main/main.c:1813
\#5 0x00000000009bc433 in do_cli (argc=2, argv=0xfabb10) at /root/php-5.5.38/sapi/cli/php_cli.c:1177
\#6 0x00000000009bcabb in main (argc=2, argv=0xfabb10) at /root/php-5.5.38/sapi/cli/php_cli.c:1378
最佳答案
我找到了另一种方法,使用zend_lookup_class
找到一个php类并对其进行实例化;
zend_class_entry **class_ce, *old_scope;
zval *retval_ptr;
zend_lookup_class( name, strlen(name), &class_ce TSRMLS_CC );
object_init_ex(return_value, *class_ce);
那么返回值现在是一个对象!