Basic principles of Reactive Extensions
Let's have a look at a very simple example of RxPHP, similar to what we did in the previous chapter, and use it to demonstrate some of the basic principles behind Reactive Extensions.
We won't bother with defining an observer right now and will focus only on Observables and operators:
// rxphp_basics_01.php use Rx\Observable; $fruits = ['apple', 'banana', 'orange', 'raspberry']; Observable::fromArray($fruits) // Observable ->map(function($value) { // operator return strlen($value); }) ->filter(function($len) { // operator return $len > 5; }) ->subscribe($observer); // observer
In this example, we have one Observable, two operators and one observer.
An Observable can be chained with operators. In this example, the operators are map()
and filter()
.
Observables have the subscribe()
method that is used by observers to start receiving values...