1--TEST-- 2Test 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 14echo "*** 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*/ 23function callback_private($value, $key, $addValue) 24{ 25 echo "value : "; 26 var_dump($value->getValue()); 27 echo "key : "; 28 var_dump($key); 29} 30 31function callback_public($value, $key) 32{ 33 echo "value : "; 34 var_dump($value->pub_value); 35} 36function callback_protected($value, $key) 37{ 38 echo "value : "; 39 var_dump($value->get_pro_value()); 40} 41 42class 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 71echo "-- For private member --\n"; 72var_dump( array_walk($input, "callback_private", 1)); 73echo "-- For public member --\n"; 74var_dump( array_walk($input, "callback_public")); 75echo "-- For protected member --\n"; 76var_dump( array_walk($input, "callback_protected")); 77 78echo "Done" 79?> 80--EXPECTF-- 81*** Testing array_walk() : array of objects *** 82-- For private member -- 83value : int(3) 84key : int(0) 85value : int(10) 86key : int(1) 87value : int(20) 88key : int(2) 89value : int(-10) 90key : int(3) 91bool(true) 92-- For public member -- 93value : int(3) 94value : int(10) 95value : int(20) 96value : int(-10) 97bool(true) 98-- For protected member -- 99value : int(3) 100value : int(10) 101value : int(20) 102value : int(-10) 103bool(true) 104Done 105