Last active
February 26, 2016 15:11
-
-
Save seromenho/42ac6536dc17894f00cb to your computer and use it in GitHub Desktop.
array_walk_recursive working for objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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