summaryrefslogtreecommitdiff
path: root/ext/standard/tests/array/array_filter_object.phpt
blob: 9b10d20aea3b37b5263432338cb3a1db6963f22b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
--TEST--
Test array_filter() function : object functionality
--FILE--
<?php
/* This file uses 'input' array with different types of objects and passes
 * it to array_filter() to test object functionality
 * i.e. object of simple class with members and functions
 * object of empty class
 * object of child class extending abstract class
 * object of class containing static member
 */

echo "*** Testing array_filter() : object functionality ***\n";

// simple class with members - variable and method
class SimpleClass
{
  public $var1 = 10;
  public function check() {
    return $var1;
  }
}

// class without members
class EmptyClass
{
}

// abstract class
abstract class AbstractClass
{
  protected $var2 = 5;
  abstract function emptyMethod();
}

// class deriving above abstract class
class ChildClass extends AbstractClass
{
  private $var3;
  public function emptyMethod() {
    echo "defined in child";
  }
}

// class with final method
class FinalClass
{
  private $var4;
  final function finalMethod() {
    echo 'This can not be overloaded';
  }
}

// class with static members
class StaticClass
{
  static $var5 = 5;
  public static function staticMethod() {
    echo 'this is static method';
  }
}

function always_true($input)
{
  return true;
}

// Callback function which returns always false
function always_false($input)
{
  return false;
}

// 'input' array containing objects as elements
$input = array(
  new SimpleClass(),
  new EmptyClass(),
  new ChildClass(),
  new FinalClass(),
  new StaticClass()
);


// with default callback
var_dump( array_filter($input) );

// with always_true callback function
var_dump( array_filter($input, "always_true") );

// with always_false callback function
var_dump( array_filter($input, "always_false") );

echo "Done"
?>
--EXPECTF--
*** Testing array_filter() : object functionality ***
array(5) {
  [0]=>
  object(SimpleClass)#%d (1) {
    ["var1"]=>
    int(10)
  }
  [1]=>
  object(EmptyClass)#%d (0) {
  }
  [2]=>
  object(ChildClass)#%d (2) {
    ["var3":"ChildClass":private]=>
    NULL
    ["var2":protected]=>
    int(5)
  }
  [3]=>
  object(FinalClass)#%d (1) {
    ["var4":"FinalClass":private]=>
    NULL
  }
  [4]=>
  object(StaticClass)#%d (0) {
  }
}
array(5) {
  [0]=>
  object(SimpleClass)#%d (1) {
    ["var1"]=>
    int(10)
  }
  [1]=>
  object(EmptyClass)#%d (0) {
  }
  [2]=>
  object(ChildClass)#%d (2) {
    ["var3":"ChildClass":private]=>
    NULL
    ["var2":protected]=>
    int(5)
  }
  [3]=>
  object(FinalClass)#%d (1) {
    ["var4":"FinalClass":private]=>
    NULL
  }
  [4]=>
  object(StaticClass)#%d (0) {
  }
}
array(0) {
}
Done