As you’ve probably noticed the way in which PHP calls it’s Higher-Order Functions is quite different from C#, Java and Swift. Basically PHP by default uses method calls that wrap the input as opposed to the C#, Java and Swift which rely on method chaining.

Here’s a Swift example of method chaining; the array of values is passed to the map using the chain dot syntax, similarly the result of that is chained to the sorted method, and ultimately to the forEach method.

Swift
1
2
3
4
5
#!/usr/bin/env swiftshell
[1, 2, 3, 4, 5, 6, 7, 8, 9]
  .map { $0 * 2 }
  .sorted(by: >)
  .forEach { print($0, terminator: ", ") }

Not only does this make it easier to understand the process flow, but also helps with the formatting of the processing block. In this example, I’ve pressed carriage return, after each method call in the chain in order to align their dots. This is a natural formatting feature in these languages.

Here’s the equivalent in Java:

Java
1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Arrays;
import java.util.stream.IntStream;

public class main 
{ 
  public static void main(String[] args) {  
    Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9)
      .map((value) -> value * 2)
      .sorted(Collections.reverseOrder())
      .forEach((value) -> System.out.print(value + ", "));
  }
}

Here’s the equivalent in PHP:

PHP
1
2
3
4
5
6
7
<?php
$answer = array_map(function ($value) {return $value*2;}, [1, 2, 3, 4, 5, 6, 7, 8, 9]);
rsort($answer);
foreach ($answer as $value) {
	print$value.",";
}
?>

It’s broken up into a few separated commands, rsort(reverse sort) in particular must be separated from array_map, because array_map returns a new array, where as rsort sorts in place i.e. we can’t merge the two. However all is not lost, you can have something quite similar to Swift, Java and C# with a bit of PHP magic methods..

To achieve this we’re going to create a new class that extends ArrayObject and implements the magic method __call:

PHP
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
<?php
namespace Functional;

use ArrayObject;
use Transversable;

/**
  Extra Higher Order Functions
  */
class HOF
{

/**
  Enumerator Sort Types 
  */
abstract class SortType
{
    const Ascending = 0;
    const Descending = 1;
    const Custom = 2;
}

/**
 Array Helper for Functional Method Chaining
 */
class FArray extends ArrayObject 
{  
  private function _arrayFunc($action, $argv)
  {
    return call_user_func_array($action, 
              array_merge(array($this->getArrayCopy()), $argv));
  }

  public static function init($array)
  {
    return new FArray($array);
  }

  public static function initSequence(...$array)
  {
    return new FArray($array);
  }

  /**
   Sort altered to return a sorted copy; to enable method chaining
   */
  private function _sort($type, $argv)
  {
    $copyOfArray = $this->getArrayCopy();
    switch ($type)
    {
      case SortType::Ascending:
        sort($copyOfArray);
        break;

      case SortType::Descending:
        rsort($copyOfArray);
        break;

      default:
        sort($copyOfArray, $argv[0]);
    }
    return new FArray($copyOfArray);
  }

  /**
    Functional Method Chaining Implementation
    
    Style Choice: 
      Method names have been shortened, 
      specifically `array_` prefix has been removed
    */
  public function __call($func, $argv)
  {
    switch ($func) 
    {
      case 'map':
        return new FArray(call_user_func_array('array_map', 
          array_merge($argv, array($this->getArrayCopy()))));

      case 'sort':
        return $this->_sort(SortType::Ascending, $argv);

      case 'rsort':
        return $this->_sort(SortType::Descending, $argv);

      case 'reduce':
        return $this->_arrayFunc('array_reduce', $argv);

      case 'product':
        return $this->_arrayFunc('array_product', $argv);

      case 'sum':
        return $this->_arrayFunc('array_sum', $argv);

      case 'foreach':
      case 'walk':
        return array_walk($this, $argv[0]);

      case 'print':
        return print "[" . join(", ", 
          $this->getArrayCopy()) . "]\n";

      case 'walkRecursive':
        return array_walk_recursive($this, $argv[0]);

      case 'replace':
        return $this->_arrayFunc('array_replace', $argv);

      case 'reverse':
        return new FArray(
          $this->_arrayFunc('array_reverse', $argv));

      case 'filter':
        return new FArray(
          $this->_arrayFunc('array_filter', $argv));

      case 'keys':
        return new FArray($this->_arrayFunc('array_keys', $argv));

      default:
        throw new BadMethodCallException(__CLASS__.'->'.$func);
    }
  }

  public function __toString() 
  {
    return '[' . join(", ", $this->getArrayCopy()) . ']';
  }
}

/**
  Custom Asserts
  Used to validate that the passed in $collection is transversable
  */
class InvalidArgumentException extends \InvalidArgumentException
{
  public static function assertCollection(
    $collection, $callee, $paramPosition)
  {
    self::assertCollectionAlike($collection, 'Traversable', 
                                $callee, $paramPosition);
  }

  private static function customMessage(
    $callee, $parameterPosition, $className)
  {
    return sprintf(
      '%s() expects parameter %d to be array or instance of %s',
      $callee,
      $paramPosition,
      $className);
  }

  private static function assertCollectionAlike(
    $collection, $className, $callee, $paramPosition)
  {
    if (!is_array($collection) && 
      !$collection instanceof $className) {
      throw new static(self::customMessage(
        $callee, $paramPosition, $className));
    }
  }
}
?>

Quite a bit of code… but I promise it’s worth it. So after adding this class to your code, you can now rewrite the PHP version as follows:

PHP
1
2
3
4
5
6
7
8
9
<?php
require_once ('Functional.php');

(new FArray([1, 2, 3, 4, 5, 6, 7, 8, 9]))
	->map(function ($v) {return $v*2;})
	->rsort()
	->foreach(function ($v) {return print$v.", ";});

?>
OUTPUT : Chained Method Calls
18, 16, 14, 12, 10, 8, 6, 4, 2,

Note:

  • in PHP method chaining is done with -> instead .
  • __call()` magic method is triggered when invoking inaccessible methods in an object context.
  • FArray can be called anything you like; and it acts as a stand-in for arrays. Similarly in the __call() method, you can name the methods anything you like; you could even choose to keep them the same as PHP’s default. i.e. instead of case map:, you could use case array_map instead.

Happy coding…