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