0
我試圖創建一個PHP擴展,它具有一個類,其中一個元素應該是一個字符串數組。當我嘗試像下面這樣做時,當我取消註釋我嘗試創建並實現test_init_test()
中的數組時,出現錯誤「PHP Fatal error: Internal zval's can't be arrays, objects or resources in Unknown on line 0
」。將數組添加到PHP擴展中的類
我要麼不理解如何創建數組,要麼我只是不理解zend_declare_property
函數。我該如何做到這一點?
#include "php.h"
#include "php_myextension.h"
#include "test.h"
zend_class_entry *test_ce_test;
static zend_function_entry test_methods[] = {
PHP_ME(Test, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
{NULL, NULL, NULL}
};
void test_init_test(TSRMLS_D) {
zend_class_entry ce;
// zval *cachedirs_array = emalloc(sizeof(zval));
// array_init(cachedirs_array);
INIT_CLASS_ENTRY(ce, "Test", test_methods);
test_ce_test = zend_register_internal_class(&ce TSRMLS_CC);
zend_declare_property_string(test_ce_test, "arch", sizeof("arch"), "", ZEND_ACC_PUBLIC TSRMLS_CC);
// zend_declare_property(test_ce_test, "cachedirs", sizeof("cachedirs"), cachedirs_array, ZEND_ACC_PUBLIC TSRMLS_CC);
// zend_declare_property(test_ce_test, "cachedirs", sizeof("cachedirs"), cachedirs_array, ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_string(test_ce_test, "dbpath", sizeof("dbpath"), "", ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_string(test_ce_test, "root", sizeof("root"), "", ZEND_ACC_PUBLIC TSRMLS_CC);
}
PHP_METHOD(Test, __construct) {
char *rootpath;
char *dbpath;
size_t rp, dp;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &rootpath, &rp, &dbpath, &dp) == FAILURE) {
RETURN_NULL()
}
object_init_ex(return_value, test_ce_test);
zend_update_property_stringl(test_ce_test, getThis(), "root", sizeof("root"), rootpath, rp TSRMLS_CC);
zend_update_property_stringl(test_ce_test, getThis(), "dbpath", sizeof("dbpath"), dbpath, dp TSRMLS_CC);
}