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