Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:06:52+00:00 2026-05-30T15:06:52+00:00

I want to realize this class into php extension: class MyClass { protected $attrs

  • 0

I want to realize this class into php extension:

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

I’ve already implemented __constructor, $attrs field, and __get method. And now I can’t figure about __set.

There is my c code:

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

Where attrs – declared protected property in init function (I’ve declared property as null, but when I add data to $attrs in constructor – property updates to be an array)

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

So my question is: how I need to update my attr field in c?
My extensions succesfully compiles, I can define properties, read them, but I can’t set them – because setted value becomes null, example:

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

I’m new to c development and I really need help.

UPD 1. I’ve tried change __set body to this:

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

And now I can set string values. If I need to make switch on each type of zval??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:06:54+00:00Added an answer on May 30, 2026 at 3:06 pm

    This should work:

    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);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to realize a construct in MS SQL that would look like this
I was under the impression that the format() function of the PHP DateTime class
Imagine I have a class in C# called Bar that has a public function
I realize this is way too far into the micro-optimization area, but I am
What I want to realize is the following: In a JSP x I have
Want the function to sort the table by HP but if duplicate HPs then
want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's
To clarify: I'm not using the gettext php module /extension, reason being because I
I want to use map mechanism, based on property attribute marking, like this: [AttributeUsage(AttributeTargets.Property
I'm doing some file validation and want to load an UploadedFile into an external

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.