Annotation of embedaddon/php/ext/standard/tests/array/array_walk_object2.phpt, revision 1.1
1.1 ! misho 1: --TEST--
! 2: Test array_walk() function : object functionality - array of objects
! 3: --FILE--
! 4: <?php
! 5: /* Prototype : bool array_walk(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() with an array of objects
! 12: */
! 13:
! 14: echo "*** Testing array_walk() : 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: new MyClass(3),
! 66: new MyClass(10),
! 67: new MyClass(20),
! 68: new MyClass(-10)
! 69: );
! 70:
! 71: echo "-- For private member --\n";
! 72: var_dump( array_walk($input, "callback_private", 1));
! 73: echo "-- For public member --\n";
! 74: var_dump( array_walk($input, "callback_public"));
! 75: echo "-- For protected member --\n";
! 76: var_dump( array_walk($input, "callback_protected"));
! 77:
! 78: echo "Done"
! 79: ?>
! 80: --EXPECTF--
! 81: *** Testing array_walk() : array of objects ***
! 82: -- For private member --
! 83: value : int(3)
! 84: key : int(0)
! 85: value : int(10)
! 86: key : int(1)
! 87: value : int(20)
! 88: key : int(2)
! 89: value : int(-10)
! 90: key : int(3)
! 91: bool(true)
! 92: -- For public member --
! 93: value : int(3)
! 94: value : int(10)
! 95: value : int(20)
! 96: value : int(-10)
! 97: bool(true)
! 98: -- For protected member --
! 99: value : int(3)
! 100: value : int(10)
! 101: value : int(20)
! 102: value : int(-10)
! 103: bool(true)
! 104: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>