diff options
-rw-r--r-- | Examples/CppClassesInPhp/check_map.php | 69 | ||||
-rw-r--r-- | src/value.cpp | 52 |
2 files changed, 81 insertions, 40 deletions
diff --git a/Examples/CppClassesInPhp/check_map.php b/Examples/CppClassesInPhp/check_map.php index cdd5345..2103bac 100644 --- a/Examples/CppClassesInPhp/check_map.php +++ b/Examples/CppClassesInPhp/check_map.php @@ -5,21 +5,51 @@ * */ -class cl1 { - public $qwe = 45615; - public $asd = "asdasdasd"; - public $zxcv = "Привет!"; // check UTF-8 chars +class cl0 { + public $cl0_float = 3.14; + public $cl0_str1 = 'public str1'; + private $cl0_str2 = 'private str2'; + protected $cl0_str3 = 'protected str3'; +} + +class cl1 extends cl0 implements arrayaccess +{ + static $cl1_static = 'static prop'; + + const CL1_CONST = 'const prop'; + + public $cl1_num = 45615; + public $cl1_str = "Public Prop"; + + private $cl1_pp1 = "Private Prop1"; + private $cl1_pp2 = "Private Prop2"; + + protected $cl1_prp1 = "Protected Prop1"; + protected $cl1_prp2 = "Protected Prop2"; + public function fn($a) { echo $a; } - function __destruct(){ - echo 'cl1::__destruct'; - } function __toString() { return 'I\'m class cl1'; } + public function offsetSet($offset, $value) { + if (!is_null($offset)) { + $this->$offset = $value; + } + } + public function offsetExists($offset) { + return isset($this->$offset); + } + public function offsetUnset($offset) { + unset($this->$offset); + } + public function offsetGet($offset) { + return isset($this->$offset) ? $this->$offset : null; + } + } class emptyClass {} @@ -34,10 +64,15 @@ $arr = array( new cl1(), '%'=>'%$%$%', ); -//$arr = array(5,17,'qwe' => 'qweqweqweqw',4=>88,'17'=>'170','1'=>4, new cl1()); -//$arr = array(3.14,2.7,11,0,500); + +$arr = new cl1(); + +$arr[5] = 55; +$arr['strstr'] = 'strstrstrstrstrstr'; + +//$arr = new emptyClass(); //$arr = array(); -//$arr = new cl1(); + $q = new MyClass(); @@ -47,17 +82,5 @@ var_export($arr); // Works for objects and arrays $q->loopObject($arr); - -$q->loopObject(new emptyClass()); - - -/* -// Validation removal (i.e. do I need to use zval_add_ref(value);) -echo "\nunset(\$arr):"; -unset($arr); -echo "\nunset(\$q):"; -unset($q); -*/ - - +//$q->loopObject($arr); diff --git a/src/value.cpp b/src/value.cpp index 2c44616..d2646f9 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -1425,6 +1425,7 @@ std::map<std::string,Php::Value> Value::mapValue() const // get access to the internal hash table of _val // see Zend/zend_API.h 723: HASH_OF(_val) HashTable *arr = isArray() ? Z_ARRVAL_P(_val) : Z_OBJ_HT_P(_val)->get_properties((_val) TSRMLS_CC); + // similarly php: reset($array): // The definition of this and the following functions can be found in Zend/zend_hash.h 174 @@ -1432,28 +1433,45 @@ std::map<std::string,Php::Value> Value::mapValue() const // If the following line to remove, then repeated calling the Value::mapValue() will return an empty map zend_hash_internal_pointer_reset(arr); - // check empty array/object - if(zend_hash_has_more_elements(arr) == FAILURE) { - return result; + if (isArray()) + { + unsigned int hash_key_type; + while( (hash_key_type = zend_hash_get_current_key(arr, &key, &ind, 0)) != HASH_KEY_NON_EXISTENT ) + { + zend_hash_get_current_data(arr, (void **) &value); + + if(HASH_KEY_IS_LONG == hash_key_type) + { + result[std::to_string(ind)] = Value(*value); + } + else // hash_key_type == HASH_KEY_IS_STRING + { + result[key] = Value(*value); + } + + // next iteration + zend_hash_move_forward(arr); + } } - - unsigned int hash_key_type; - while( (hash_key_type = zend_hash_get_current_key(arr, &key, &ind, 0)) != HASH_KEY_NON_EXISTENT ) + else { - zend_hash_get_current_data(arr, (void **) &value); - - if(HASH_KEY_IS_LONG == hash_key_type) + // For obtaining a hashtable of the object meets function void rebuild_object_properties(zend_object *zobj) + // Zend/zend_object_handlers.c 66 + // hashtable of object's properties always has string (no integer) keys + while( zend_hash_get_current_key(arr, &key, &ind, 0) != HASH_KEY_NON_EXISTENT ) { - result[std::to_string(ind)] = Value(*value); - } - else // hash_key_type == HASH_KEY_IS_STRING - { - result[key] = Value(*value); + // if propertie is accessible (i.e. propertie access type is public. See rebuild_object_properties ) + if('\0' != *key) + { + zend_hash_get_current_data(arr, (void **) &value); + result[key] = Value(*value); + } + + // next iteration + zend_hash_move_forward(arr); } - - // next iteration - zend_hash_move_forward(arr); } + } // done |