Internal zvals can't be arrays

php.internals

David Sklar

22 years ago
zend_declare_property() tells me that I can't use it to create a class property that is an array. Why is that? What is the recommended way to create an array class property? Thanks, David

Marcus Börger

22 years ago
Hello David, i did most of these functions but have no idea how to create default arrays. If you find out please tell us. marcus Thursday, May 13, 2004, 9:29:02 PM, you wrote:
> zend_declare_property() tells me that I can't use it to create a class > property that is an array. Why is that? What is the recommended way to > create an array class property?
> Thanks, > David
-- Best regards, Marcus mailto:helly@php.net

David Sklar

22 years ago
I think your humor is too subtle for me. I don't understand what you mean. If I do this: PHP_METHOD(test_class,__construct); zend_class_entry *test_class_ce_ptr = NULL; static zend_function_entry test_class_functions[] = { PHP_ME(test_class, __construct, NULL, ZEND_ACC_PUBLIC) {NULL, NULL, NULL} }; And then I put this in my MINIT function: INIT_CLASS_ENTRY(ce, "test_class", test_class_functions); test_class_ce_ptr = zend_register_internal_class(&ce TSRMLS_CC); zend_declare_property_null(test_class_ce_ptr, "test_property", sizeof("test_property")-1, ZEND_ACC_PUBLIC TSRMLS_CC); And I make test_class's constructor look like this: PHP_METHOD(test_class, __construct) { zval *tmp = NULL; ALLOC_ZVAL(tmp); tmp->is_ref = 0; tmp->refcount = 0; array_init(tmp); add_next_index_long(tmp, 100); zend_update_property(test_class_ce_ptr, this_ptr, "test_property", sizeof("test_property")-1, tmp TSRMLS_CC); } Then this PHP program: $e = new test_class(); print new ReflectionClass($e); var_dump($e); Prints: Class [ <internal> class test_class ] { - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [1] { Property [ <default> public $test_property ] } - Methods [1] { Method [ <internal> <ctor> public method __construct ] { } } } object(test_class)#1 (1) { ["test_property"]=> array(1) { [0]=> int(100) } } So it would be handy (since all the property creation would be in one place and there wouldn't be the confusing inference from reading the code that the property is NULL) if I could create the array zval in MINIT instead of in the constructor. David Marcus Boerger wrote:

Marcus Börger

22 years ago
Hello David, Thursday, May 13, 2004, 10:42:00 PM, you wrote:
> I think your humor is too subtle for me. I don't understand what you > mean. If I do this:
sorry i just wanted to say that if you find out a solution you should tell it to me and that atm i don't have any idea how to do it. [...] Your code was absolute correct.
> So it would be handy (since all the property creation would be in one > place and there wouldn't be the confusing inference from reading the > code that the property is NULL) if I could create the array zval in > MINIT instead of in the constructor.
Yes youle be handy. Especially when we have typehinting for arrays. regards marcus

David Sklar

22 years ago
>>I think your humor is too subtle for me. I don't understand what you >>mean. If I do this: > > > sorry i just wanted to say that if you find out a solution you should > tell it to me and that atm i don't have any idea how to do it. > > [...] > > Your code was absolute correct. >
OK, I figured that since you wrote the functions, if you couldn't figure it out then I was not going to be able to! :) Thanks, David