1--TEST--
2Test 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
14echo "*** 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*/
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  array(
66  new MyClass(3),
67  new MyClass(10),
68  ),
69  new MyClass(20),
70  array(new MyClass(-10))
71);
72
73echo "-- For private member --\n";
74var_dump( array_walk_recursive($input, "callback_private", 1));
75echo "-- For public member --\n";
76var_dump( array_walk_recursive($input, "callback_public"));
77echo "-- For protected member --\n";
78var_dump( array_walk_recursive($input, "callback_protected"));
79
80echo "Done"
81?>
82--EXPECT--
83*** Testing array_walk_recursive() : array of objects ***
84-- For private member --
85value : int(3)
86key : int(0)
87value : int(10)
88key : int(1)
89value : int(20)
90key : int(1)
91value : int(-10)
92key : int(0)
93bool(true)
94-- For public member --
95value : int(3)
96value : int(10)
97value : int(20)
98value : int(-10)
99bool(true)
100-- For protected member --
101value : int(3)
102value : int(10)
103value : int(20)
104value : int(-10)
105bool(true)
106Done
107