如何在 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 successn");
object_init_ex(obj, foo_ce);
} else {
printf("get class failedn");
}
printf("heren");
但是当它进入object_init_ex
片段时,我得到了Segmentation fault
!
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);
那么return_value现在是一个对象!