Skip to content

Add keys method for Php::Value - #115

Open
andot wants to merge 2 commits into
CopernicaMarketingSoftware:masterfrom
andot:keys
Open

Add keys method for Php::Value#115
andot wants to merge 2 commits into
CopernicaMarketingSoftware:masterfrom
andot:keys

Conversation

@andot

@andot andot commented Jul 10, 2014

Copy link
Copy Markdown
Contributor

This method can get the array keys, or object property names, include private & protected properties.

It is similar to working in PHP:

array_keys($array);

and

array_keys((array)$object);

but the return value type is std::vectorPhp::Value, and it's faster than using php function.

This method can get the array keys, or object property names, include
private & protected properties.

It is similar to working in PHP:

array_keys($array);

and

array_keys((array)$object);

but the return value type is std::vector<Php::Value>, and it's faster
than using php function.
@andot andot closed this Jul 10, 2014
@andot
andot deleted the keys branch July 10, 2014 09:37
@andot
andot restored the keys branch July 10, 2014 09:38
@andot andot reopened this Jul 10, 2014
@valmat

valmat commented Jul 10, 2014

Copy link
Copy Markdown
Contributor

I think this feature is partly duplicates code of valueiterator: .../include/valueiterator.h .../zend/hashiterator.h.
Now the same can be done like so:

std::vector<Php::Value> keys;
keys.reserve(array.size());
for(auto &it: array)
{
    keys.push_back(it.first);
}

@andot

andot commented Jul 10, 2014

Copy link
Copy Markdown
Contributor Author

The HashIterator has a bug, if the array has a reference item, for(auto &it: array) will break the array. for example:

Php::Value getKeys(Php::Parameters &params) {
    Php::Value array = params[0];
    std::vector<Php::Value> keys;
    keys.reserve(array.size());
    for(auto &it: array)
    {
        keys.push_back(it.first);
    }
}
<?php
   $a = array();
   $a[] = &$a;
   $a[] = "hello";
   var_dump($a);
   var_dump(getKeys($a));
   var_dump($a);
?>

the result is:

array(2) {
  [0]=>
  &array(2) {
    [0]=>
    *RECURSION*
    [1]=>
    string(5) "hello"
  }
  [1]=>
  string(5) "hello"
}
array(2) {
  [0]=>
  int(0)
  [1]=>
  int(1)
}
NULL

@andot

andot commented Jul 10, 2014

Copy link
Copy Markdown
Contributor Author

and when use iterator on an object Value, we can only get the public properties. But the keys method can get all properties(include private and protected properites).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants