Higher-order Observables
When talking about the prerequisites for functional programming we mentioned higher-order functions. These are functions that return other functions. A very similar concept is applied in RxPHP as well, when using Observables.
A higher-order Observable is an Observable that emits other Observables. To illustrate how higher-order Observables differ from first-order Observables, consider the following simple example:
// higher_order_01.php use Rx\Observable; Observable::range(1, 3) ->subscribe(new DebugSubject());
This example just prints three values and completes as expected. This is what we expect from any first-order Observable:
$ php higher_order_01.php 22:54:05 [] onNext: 1 (integer) 22:54:05 [] onNext: 2 (integer) 22:54:05 [] onNext: 3 (integer) 22:54:05 [] onCompleted
Now, we can make this more complicated by adding the map()
operator that, instead of returning an integer, returns another Observable:
// higher_order_02.php ...