D
D
dds2013-05-01 15:10:38
PHP
dds, 2013-05-01 15:10:38

Problem with duplicating properties in a class created in a C-extension?

Hello everyone,
I just recently started developing extensions for PHP and I can not understand why there is a duplication of properties in the class created in the extension. When creating a class, I do this:

STOPWATCH_INIT_CLASS(StopwatchEventNew) {
  STOPWATCH_REGISTER_CLASS(Symfony\\Component\\Stopwatch, StopwatchEventNew, event, stopwatch_event_method_entry, 0);

  zend_declare_property_string(stopwatch_event_ce, "category", sizeof("category")-1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
  zend_declare_property_null(stopwatch_event_ce,   "started",  sizeof("started")-1, ZEND_ACC_PUBLIC TSRMLS_CC);
  zend_declare_property_null(stopwatch_event_ce,   "periods",  sizeof("periods")-1,  ZEND_ACC_PRIVATE TSRMLS_CC);
  zend_declare_property_double(stopwatch_event_ce, "origin",   sizeof("origin")-1, 0,  ZEND_ACC_PRIVATE TSRMLS_CC);

  return SUCCESS;
}

#define STOPWATCH_INIT_CLASS(name) int stopwatch_ ##name## _init(INIT_FUNC_ARGS)

#define STOPWATCH_REGISTER_CLASS(ns, class_name, name, methods, flags) \
  { \
    zend_class_entry ce; \
    memset(&ce, 0, sizeof(zend_class_entry)); \
    INIT_NS_CLASS_ENTRY(ce, #ns, #class_name, methods); \
    stopwatch_ ##name## _ce = zend_register_internal_class(&ce TSRMLS_CC); \
    stopwatch_ ##name## _ce->ce_flags |= flags;  \
  }

If I do a var_dump for an instance of this class, then it shows me 4 fields of the class, but when I add the constructor implementation:
PHP_METHOD(StopwatchEventNew, __construct)
{
  zend_class_entry *ce;
  double origin;
  char *category;
  int category_len;
  zval *started, *periods;

  if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d|s", &origin, &category, &category_len) == FAILURE)
  {
    return;
  }

  ce = Z_OBJCE_P(this_ptr);

        zend_update_property_string(ce, this_ptr, "category", sizeof("category"), category TSRMLS_CC);
  zend_update_property_double(ce, this_ptr, "origin", sizeof("origin"), formatTime(origin TSRMLS_CC) TSRMLS_CC);

  MAKE_STD_ZVAL(started);
  MAKE_STD_ZVAL(periods);
  array_init(started);
  array_init(periods);

  zend_update_property(ce, this_ptr, "started", sizeof("started"), started TSRMLS_CC);
  zend_update_property(ce, this_ptr, "periods", sizeof("periods"), periods TSRMLS_CC);
}

then when var_dump of a new instance shows me 8 fields, where 4 with default values ​​and 4 with updated ones. When I make a getter on any field, then
val = zend_read_property(ce, this_ptr, "category", sizeof("category"), FALSE TSRMLS_CC);

returns me the correct value passed to the constructor, but I don't understand why the fields are duplicated.
Can someone explain to me where I have a problem? maybe it's not worth adding fields to it when creating a class? and initialize them all in the constructor?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dds, 2013-05-07
@dds

After a week's rest from the task, I found an error. In general, as I thought, the mistake itself is very stupid).
When creating the class I used:

sizeof("category")-1

and when reading/updating:
sizeof("category")

Therefore, 2 different variables were obtained.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question