2012-02-28 67 views
3

我想實現這個類爲PHP擴展不能更新類字段:PHP擴展:使用zend_hash_update

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 - 在初始化函數聲明爲protected屬性(我聲明的屬性爲空,但是當我在構造函數中添加數據$ ATTRS - 屬性更新是一個數組)

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

所以我的問題是:我需要如何更新我的attr字段在c? 我成功地擴展編譯,我可以定義的屬性,一定要讀,但我不能將它們設置 - 因爲設定值不變變爲零,例如:

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?

回答

3

這應該工作:

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); 
    } 
}