1--TEST--
2Test usort() function : object functionality - Different types of classes
3--FILE--
4<?php
5/* Prototype  : bool usort(array $array_arg, string $cmp_function)
6 * Description: Sort an array by values using a user-defined comparison function
7 * Source code: ext/standard/array.c
8 */
9
10/*
11 * Pass an array of objects which are either:
12 * 1. Empty
13 * 2. Static
14 * 2. Inherited
15 * to test behaviour of usort()
16 */
17
18echo "*** Testing usort() : object functionality ***\n";
19
20function cmp_function($value1, $value2)
21{
22  if($value1 == $value2) {
23    return 0;
24  }
25  else if($value1 > $value2) {
26    return 1;
27  }
28  else
29    return -1;
30}
31
32// Class without any member
33class EmptyClass
34{
35}
36
37// Class with static member
38class StaticClass
39{
40  public static $static_value;
41  public function __construct($value) {
42    StaticClass::$static_value = $value;
43  }
44}
45
46// Abstract class
47abstract class AbstractClass
48{
49  public $pub_value;
50  public abstract function abstractMethod();
51}
52
53// Child class extending abstract class
54class ChildClass extends AbstractClass
55{
56  public $child_value = 100;
57  public function abstractMethod() {
58    $pub_value = 5;
59  }
60  public function __construct($value) {
61    $this->child_value = $value;
62  }
63}
64
65// Testing uasort with StaticClass objects as elements of 'array_arg'
66echo "-- Testing usort() with StaticClass objects --\n";
67$array_arg = array(
68  0 => new StaticClass(20),
69  1 => new StaticClass(50),
70  2 => new StaticClass(15),
71  3 => new StaticClass(70),
72);
73var_dump( usort($array_arg, 'cmp_function') );
74var_dump($array_arg);
75
76// Testing uasort with EmptyClass objects as elements of 'array_arg'
77echo "-- Testing usort() with EmptyClass objects --\n";
78$array_arg = array(
79  0 => new EmptyClass(),
80  1 => new EmptyClass(),
81  2 => new EmptyClass(),
82  3 => new EmptyClass(),
83);
84var_dump( usort($array_arg, 'cmp_function') );
85var_dump($array_arg);
86
87// Testing uasort with ChildClass objects as elements of 'array_arg'
88echo "-- Testing usort() with ChildClass objects --\n";
89$array_arg = array(
90  0 => new ChildClass(20),
91  1 => new ChildClass(500),
92  2 => new ChildClass(15),
93  3 => new ChildClass(700),
94);
95var_dump( usort($array_arg, 'cmp_function') );
96var_dump($array_arg);
97?>
98===DONE===
99--EXPECTF--
100*** Testing usort() : object functionality ***
101-- Testing usort() with StaticClass objects --
102bool(true)
103array(4) {
104  [0]=>
105  object(StaticClass)#%d (0) {
106  }
107  [1]=>
108  object(StaticClass)#%d (0) {
109  }
110  [2]=>
111  object(StaticClass)#%d (0) {
112  }
113  [3]=>
114  object(StaticClass)#%d (0) {
115  }
116}
117-- Testing usort() with EmptyClass objects --
118bool(true)
119array(4) {
120  [0]=>
121  object(EmptyClass)#%d (0) {
122  }
123  [1]=>
124  object(EmptyClass)#%d (0) {
125  }
126  [2]=>
127  object(EmptyClass)#%d (0) {
128  }
129  [3]=>
130  object(EmptyClass)#%d (0) {
131  }
132}
133-- Testing usort() with ChildClass objects --
134bool(true)
135array(4) {
136  [0]=>
137  object(ChildClass)#%d (2) {
138    ["child_value"]=>
139    int(15)
140    ["pub_value"]=>
141    NULL
142  }
143  [1]=>
144  object(ChildClass)#%d (2) {
145    ["child_value"]=>
146    int(20)
147    ["pub_value"]=>
148    NULL
149  }
150  [2]=>
151  object(ChildClass)#%d (2) {
152    ["child_value"]=>
153    int(500)
154    ["pub_value"]=>
155    NULL
156  }
157  [3]=>
158  object(ChildClass)#%d (2) {
159    ["child_value"]=>
160    int(700)
161    ["pub_value"]=>
162    NULL
163  }
164}
165===DONE===
166