array_map_object3.phpt 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. --TEST--
  2. Test array_map() function : object functionality - class methods as callback function
  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 functionality with following callback function variations:
  11. * 1) child class method using parent object
  12. * 2) parent class method using child object
  13. * 3) child class method using parent class
  14. * 4) parent class method using child class
  15. */
  16. echo "*** Testing array_map() : class methods as callback function ***\n";
  17. $arr1 = array(1, 5, 7);
  18. class ParentClass
  19. {
  20. public $var1 = 10;
  21. public static function staticParent1($n) {
  22. return $n;
  23. }
  24. private static function staticParent2($n) {
  25. return $n;
  26. }
  27. }
  28. class ChildClass extends ParentClass
  29. {
  30. var $parent_obj;
  31. public function __construct ( ) {
  32. $this->parent_obj = new ParentClass();
  33. }
  34. public $var2 = 5;
  35. public static function staticChild($n) {
  36. return $n;
  37. }
  38. public function nonstaticChild($n) {
  39. return $n;
  40. }
  41. }
  42. $childobj = new ChildClass();
  43. $parentobj = new ParentClass();
  44. echo "-- accessing parent method from child class --\n";
  45. var_dump( array_map(array('ChildClass', 'staticParent1'), $arr1) );
  46. echo "-- accessing child method from parent class --\n";
  47. var_dump( array_map(array('ParentClass', 'staticChild'), $arr1) );
  48. echo "-- accessing parent method using child class object --\n";
  49. var_dump( array_map(array($childobj, 'staticParent1'), $arr1) );
  50. echo "-- accessing child method using parent class object --\n";
  51. var_dump( array_map(array($parentobj, 'staticChild'), $arr1) );
  52. echo "Done";
  53. ?>
  54. --EXPECTF--
  55. *** Testing array_map() : class methods as callback function ***
  56. -- accessing parent method from child class --
  57. array(3) {
  58. [0]=>
  59. int(1)
  60. [1]=>
  61. int(5)
  62. [2]=>
  63. int(7)
  64. }
  65. -- accessing child method from parent class --
  66. Warning: array_map() expects parameter 1 to be a valid callback, class 'ParentClass' does not have a method 'staticChild' in %s on line %d
  67. NULL
  68. -- accessing parent method using child class object --
  69. array(3) {
  70. [0]=>
  71. int(1)
  72. [1]=>
  73. int(5)
  74. [2]=>
  75. int(7)
  76. }
  77. -- accessing child method using parent class object --
  78. Warning: array_map() expects parameter 1 to be a valid callback, class 'ParentClass' does not have a method 'staticChild' in %s on line %d
  79. NULL
  80. Done