Annotation of embedaddon/php/ext/standard/tests/array/array_map_object1.phpt, revision 1.1.1.1

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>