1--TEST-- 2Test array_map() function : usage variations - object functionality 3--FILE-- 4<?php 5/* 6 * Testing array_map() for object functionalities: 7 * 1) simple class with variable and method 8 * 2) class without members 9 * 3) class with only one method and no variable 10 * 4) abstract and child class 11 * 5) class with static and final members 12 * 6) interface and implemented class 13 */ 14echo "*** Testing array_map() : object functionality ***\n"; 15 16echo "-- simple class with public variable and method --\n"; 17class SimpleClass 18{ 19 public $var1 = 1; 20 public static function square($n) { 21 return $n * $n; 22 } 23} 24function test($cb, $args) { 25 echo join('::', $cb) . "\n"; 26 try { 27 var_dump(array_map($cb, $args)); 28 } catch (TypeError $e) { 29 echo $e->getMessage(), "\n"; 30 } 31} 32test(array('SimpleClass', 'square'), array(1, 2)); 33 34echo "\n-- simple class with private variable and method --\n"; 35class SimpleClassPri 36{ 37 private $var1 = 10; 38 private static function add($n) { 39 return $var + $n; 40 } 41} 42test(array('SimpleClassPri', 'add'), array(1)); 43 44echo "\n-- simple class with protected variable and method --\n"; 45class SimpleClassPro 46{ 47 protected $var1 = 5; 48 protected static function mul($n) { 49 return $var1 * $n; 50 } 51} 52test(array('SimpleClassPro', 'mul'), array(2)); 53 54echo "\n-- class without members --\n"; 55class EmptyClass 56{ 57} 58test(array('EmptyClass'), array(1, 2)); 59 60echo "\n-- abstract class --\n"; 61abstract class AbstractClass 62{ 63 protected $var2 = 5; 64 abstract static function emptyFunction(); 65} 66 67// class deriving the above abstract class 68class ChildClass extends AbstractClass 69{ 70 private $var3; 71 public static function emptyFunction() { 72 echo "defined in child\n"; 73 } 74} 75test(array('ChildClass', 'emptyFunction'), array(1, 2)); 76 77echo "\n-- class with final method --\n"; 78class FinalClass 79{ 80 private $var4; 81 final static function finalMethod() { 82 echo "This function can't be overloaded\n"; 83 } 84} 85test(array('FinalClass', 'finalMethod'), array(1, 2)); 86 87echo "\n-- class with static members --\n"; 88class StaticClass 89{ 90 static $var5 = 2; 91 public static function square($n) { 92 return ($n * $n); 93 } 94 private static function cube($n) { 95 return ($n * $n * $n); 96 } 97 protected static function retVal($n) { 98 return array($n); 99 } 100} 101test(array('StaticClass', 'square'), array(1, 2)); 102test(array('StaticClass', 'cube'), array(2)); 103test(array('StaticClass', 'retVal'), array(3, 4)); 104 105echo "-- class implementing an interface --\n"; 106interface myInterface 107{ 108 public function toImplement(); 109} 110class InterClass implements myInterface 111{ 112 public static function square($n) { 113 return ($n * $n); 114 } 115 public function toImplement() { 116 return 1; 117 } 118} 119test(array('InterClass', 'square'), array(1, 2)); 120 121?> 122--EXPECT-- 123*** Testing array_map() : object functionality *** 124-- simple class with public variable and method -- 125SimpleClass::square 126array(2) { 127 [0]=> 128 int(1) 129 [1]=> 130 int(4) 131} 132 133-- simple class with private variable and method -- 134SimpleClassPri::add 135array_map(): Argument #1 ($callback) must be a valid callback or null, cannot access private method SimpleClassPri::add() 136 137-- simple class with protected variable and method -- 138SimpleClassPro::mul 139array_map(): Argument #1 ($callback) must be a valid callback or null, cannot access protected method SimpleClassPro::mul() 140 141-- class without members -- 142EmptyClass 143array_map(): Argument #1 ($callback) must be a valid callback or null, array must have exactly two members 144 145-- abstract class -- 146ChildClass::emptyFunction 147defined in child 148defined in child 149array(2) { 150 [0]=> 151 NULL 152 [1]=> 153 NULL 154} 155 156-- class with final method -- 157FinalClass::finalMethod 158This function can't be overloaded 159This function can't be overloaded 160array(2) { 161 [0]=> 162 NULL 163 [1]=> 164 NULL 165} 166 167-- class with static members -- 168StaticClass::square 169array(2) { 170 [0]=> 171 int(1) 172 [1]=> 173 int(4) 174} 175StaticClass::cube 176array_map(): Argument #1 ($callback) must be a valid callback or null, cannot access private method StaticClass::cube() 177StaticClass::retVal 178array_map(): Argument #1 ($callback) must be a valid callback or null, cannot access protected method StaticClass::retVal() 179-- class implementing an interface -- 180InterClass::square 181array(2) { 182 [0]=> 183 int(1) 184 [1]=> 185 int(4) 186} 187