编程语言
首页 > 编程语言> > PHP扩展名:无法使用zend_hash_update更新类字段

PHP扩展名:无法使用zend_hash_update更新类字段

作者:互联网

我想实现此类到php扩展:

class MyClass {
  protected $attrs = array();
  public function __construct($id = null) {
    $this->attrs['id'] = $id;
    $this->attrs['name'] = '';
  }
  public function __get($key) {
    if (array_key_exists($key, $this->attr)) 
      return $this->attrs[$key];
  }
  public function __set($key, $value) {
    if (array_key_exists($key, $this->attr)) 
      $this->attrs[$key] = $value;
  }
}

我已经实现了__constructor,$attrs字段和__get方法.现在我不知道__set了.

有我的C代码:

PHP_METHOD(MyClass, __set) {    
  char *key;
  int key_len;
  zval *value;  

  if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &key, &key_len, &value)) {
    RETURN_NULL();
  }

  zval *attrs, *obj;
  obj = getThis();
  attrs = zend_read_property(Z_OBJCE_P(obj), obj, "attrs", strlen("attrs"), TRUE, TSRMLS_C);

  if (Z_TYPE_P(attrs) == IS_ARRAY && zend_hash_exists(Z_ARRVAL_P(attrs), key, strlen(key) + 1)) {
    zend_hash_update(Z_ARRVAL_P(attributes), key, strlen(key) + 1, &value, sizeof(zval*), NULL);
    }
  else {        
    zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 1, TSRMLS_C, "unknown field \"%s\"", key);
  }
}

attrs-初始化函数中声明的受保护属性(我将属性声明为null,但是当我向构造函数中的$attrs添加数据时-属性更新为数组)

zend_declare_property_null(myclass_ce, "attrs", strlen("attrs"), ZEND_ACC_PROTECTED TSRMLS_CC);

所以我的问题是:我该如何在c中更新attr字段?
我的扩展成功编译,我可以定义属性,读取它们,但不能设置它们-因为设置的值变为null,例如:

class MyClass2 extends MyClass {
  public function __construct($id = null) {
    parent::__construct($id);
    $this->attrs["type"] = "clz";
  }
} 

$c = new MyClass();
var_dump($c->type); // string(3) "clz"
$c->type = "myclz"; // no error, my __set method handles this call, and I'm sure I'm getting correct value 
var_dump($c->type); // NULL

我是c开发的新手,我真的需要帮助.

UPD 1.我尝试将__set主体更改为此:

zval *strval;
MAKE_STD_ZVAL(strval);
ZVAL_STRING(strval, Z_STRVAL_P(value), TRUE);
if (Z_TYPE_P(attributes) == IS_ARRAY && zend_hash_exists(Z_ARRVAL_P(attributes), key, strlen(key) + 1)) {
  zend_hash_update(HASH_OF(attributes), key, strlen(key) + 1, &strval, sizeof(zval*), NULL);
}

现在,我可以设置字符串值了.如果我需要打开每种类型的zval?

解决方法:

这应该工作:

PHP_METHOD(MyClass, __set) {    
  char *key;
  int key_len;
  zval *value, *copied;  

  if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &key, &key_len, &value)) {
    RETURN_NULL();
  }

  zval *attrs, *obj;
  obj = getThis();
  attrs = zend_read_property(Z_OBJCE_P(obj), obj, "attrs", strlen("attrs"), TRUE, TSRMLS_C);

  MAKE_STD_ZVAL(copied);
  *copied = *value;
  zval_copy_ctor(copied);

  if (Z_TYPE_P(attrs) == IS_ARRAY && zend_hash_exists(Z_ARRVAL_P(attrs), key, strlen(key) + 1)) {
    zend_hash_update(Z_ARRVAL_P(attributes), key, strlen(key) + 1, &copied, sizeof(zval*), NULL);
  }
  else {        
    zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 1, TSRMLS_C, "unknown field \"%s\"", key);
  }
}

标签:php-internals,c-3,php
来源: https://codeday.me/bug/20191201/2082575.html