Annotation of embedaddon/php/ext/standard/tests/array/array_walk_recursive_object2.phpt, revision 1.1
1.1 ! misho 1: --TEST--
! 2: Test array_walk_recursive() function : object functionality - array of objects
! 3: --FILE--
! 4: <?php
! 5: /* Prototype : bool array_walk_recursive(array $input, string $funcname [, mixed $userdata])
! 6: * Description: Apply a user function to every member of an array
! 7: * Source code: ext/standard/array.c
! 8: */
! 9:
! 10: /*
! 11: * Testing array_walk_recursive() with an array of objects
! 12: */
! 13:
! 14: echo "*** Testing array_walk_recursive() : array of objects ***\n";
! 15:
! 16: /*
! 17: * Prototype : callback(mixed $value, mixed $key, int $addvalue
! 18: * Parameters : $value - values in given input array
! 19: * $key - keys in given input array
! 20: * $addvalue - value to be added
! 21: * Description : Function adds the addvalue to each element of an array
! 22: */
! 23: function callback_private($value, $key, $addValue)
! 24: {
! 25: echo "value : ";
! 26: var_dump($value->getValue());
! 27: echo "key : ";
! 28: var_dump($key);
! 29: }
! 30:
! 31: function callback_public($value, $key)
! 32: {
! 33: echo "value : ";
! 34: var_dump($value->pub_value);
! 35: }
! 36: function callback_protected($value, $key)
! 37: {
! 38: echo "value : ";
! 39: var_dump($value->get_pro_value());
! 40: }
! 41:
! 42: class MyClass
! 43: {
! 44: private $pri_value;
! 45: public $pub_value;
! 46: protected $pro_value;
! 47: public function __construct($setVal)
! 48: {
! 49: $this->pri_value = $setVal;
! 50: $this->pub_value = $setVal;
! 51: $this->pro_value = $setVal;
! 52: }
! 53: public function getValue()
! 54: {
! 55: return $this->pri_value;
! 56: }
! 57: public function get_pro_value()
! 58: {
! 59: return $this->pro_value;
! 60: }
! 61: };
! 62:
! 63: // array containing objects of MyClass
! 64: $input = array (
! 65: array(
! 66: new MyClass(3),
! 67: new MyClass(10),
! 68: ),
! 69: new MyClass(20),
! 70: array(new MyClass(-10))
! 71: );
! 72:
! 73: echo "-- For private member --\n";
! 74: var_dump( array_walk_recursive($input, "callback_private", 1));
! 75: echo "-- For public member --\n";
! 76: var_dump( array_walk_recursive($input, "callback_public"));
! 77: echo "-- For protected member --\n";
! 78: var_dump( array_walk_recursive($input, "callback_protected"));
! 79:
! 80: echo "Done"
! 81: ?>
! 82: --EXPECTF--
! 83: *** Testing array_walk_recursive() : array of objects ***
! 84: -- For private member --
! 85: value : int(3)
! 86: key : int(0)
! 87: value : int(10)
! 88: key : int(1)
! 89: value : int(20)
! 90: key : int(1)
! 91: value : int(-10)
! 92: key : int(0)
! 93: bool(true)
! 94: -- For public member --
! 95: value : int(3)
! 96: value : int(10)
! 97: value : int(20)
! 98: value : int(-10)
! 99: bool(true)
! 100: -- For protected member --
! 101: value : int(3)
! 102: value : int(10)
! 103: value : int(20)
! 104: value : int(-10)
! 105: bool(true)
! 106: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>