array_map_object1.phpt 5.0 KB

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