Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keys method for Php::Value #115

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Add keys method for Php::Value #115

wants to merge 2 commits into from

Conversation

andot
Copy link
Contributor

@andot andot commented Jul 10, 2014

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
Copy link
Contributor

valmat commented Jul 10, 2014

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
Copy link
Contributor Author

andot commented Jul 10, 2014

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
Copy link
Contributor Author

andot commented Jul 10, 2014

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