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