Skip to content

Instantly share code, notes, and snippets.

@seromenho
Last active February 26, 2016 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save seromenho/42ac6536dc17894f00cb to your computer and use it in GitHub Desktop.
Save seromenho/42ac6536dc17894f00cb to your computer and use it in GitHub Desktop.
array_walk_recursive working for objects.
<?php
class A
{
private $ab = 5;
public $ac = 6;
}
/**
* WARNING: This https://bugs.php.net/bug.php?id=45937 seems not yet solved
* So, with this approach you could be accessing/changing private/protected
* members of the object.
*/
function walking_objects(&$value) {
if (is_object($value)) {
array_walk_recursive($value, "walking_objects");
} else {
$value = 20;
}
};
// $var can be an array or object
$var = array(new A(), 1, "c");
array_walk_recursive($var, "walking_objects");
print_r($var);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment