This document discusses PHP reactive programming. It defines reactive programming as a declarative programming paradigm concerned with data streams and propagation of change. It describes common programming paradigms like imperative, declarative, sequential, parallel, asynchronous, and functional programming. It also discusses the observer design pattern and how ReactiveX libraries like RxPHP make reactive programming easier. RxPHP uses observables, operators, and other components to express data flows and automatically propagate changes.
4. Reactive programming is a declarative
programming paradigm concerned with
data streams and the propagation of change.
This means that it becomes possible to express
static (e.g. arrays) or dynamic
(e.g. event emitters) data streams with ease
via the employed programming language(s)
https://en.wikipedia.org/wiki/Reactive_programming
Reactive Programming
6. This is a set of concepts defining a style
of building and structuring programs.
Most programming languages, such as PHP,
support multiple paradigms.
We can also think of it as a mindset and a way
we approach problems when using such paradigms.
PHP Reactive Programming Book (Martin Sikora)
Programming Paradigm
8. Imperative programming is a programming paradigm
around executing statements that change
the program's state.
Statements:
Units of action with side effects in
imperative programming evaluated in sequences
usually containing expressions.
PHP Reactive Programming Book (Martin Sikora)
Imperative Paradigm
9. Example: $x = 4 + 4;
The expected side effect is assigning the value 7
to the $x variable
State:
Values of program variables in memory at any given time.
In imperative programming, we define a series
of statements that control the program's flow
and, therefore, change its state.
PHP Reactive Programming Book (Martin Sikora)
Imperative Paradigm
10. Declarative programming is a paradigm focused
on describing a program's logic instead of particular
executional steps. In other words, in declarative
programming, we define what we want instead
of how we want it”
PHP Reactive Programming Book (Martin Sikora)
Declarative Paradigm
11. Example: SQL and HTML
In SQL, we define what data from what table we want to
query, but the implementation details
are completely hidden for us. We don't even want to worry
about how the database engine stores or indexes the data
PHP Reactive Programming Book (Martin Sikora)
Declarative Paradigm
12. In sequential programming, we're executing processes
in order. This means that a process is started
when the preceding process has finished
In other words, there is always only one process
being executed.
PHP Reactive Programming Book (Martin Sikora)
Sequential & Paralel Programming
13. In parallel programming, multiple processes
can be executed concurrently:
PHP Reactive Programming Book (Martin Sikora)
Sequential & Paralel Programming
14. The term asynchronous programming
is very common in languages such as
JavaScript.
A very general definition is that,
in asynchronous programming,
we're executing code in a different order
than it was defined.
This is typical for any event based application
PHP Reactive Programming Book (Martin Sikora)
Aysnchronous Programming
15. “For example, in JavaScript, we first define
an event listener with its handler,
which is executed some time later,
when an appropriate event occurs.
PHP Reactive Programming Book (Martin Sikora)
Aysnchronous Programming
16. In PHP, this could be, for example, a web
application that needs to send an e-mail
when we create a new blog article.
PHP Reactive Programming Book (Martin Sikora)
Aysnchronous Programming
17. A very common misconception is
that asynchronous and parallel programming
are the same, or that one is an implication
of the other.
This is very common in JavaScript where,
from the user's perspective,
it looks like things are running in parallel.
PHP Reactive Programming Book (Martin Sikora)
Aysnchronous Programming
18. The functional programming paradigm treats
program flow as an evaluation of functions.
It utilizes several concepts, where
the most important for us are eliminating
side effects, avoiding mutable data,
functions as first-class citizens
and higher-order functions.
PHP Reactive Programming Book (Martin Sikora)
Functional Programming
19. The output of each function is
dependent only on its input argument values,
therefore, calling the same function twice
has to always return the same value.
It’s based on declarative programming,
in the sense of using expressions
instead of statements.
PHP Reactive Programming Book (Martin Sikora)
Functional Programming
20. In programming languages,
stating that type/object/function
is a first-class citizen (or first-class element)
means that this entity supports operations
generally available to all other entities.
Usually, this includes:
• It can be passed as a parameter to functions
• It can be returned from a function
• It can be assigned to a variable
PHP Reactive Programming Book (Martin Sikora)
Functional Programming
21. The Higher-order functions have
a very similar meaning and have to do
at least one of these
• Take a function as an argument
• Return a function as a result
In functional programming, this concept of
higher-order function is often used
in connection with methods on collections
such as map(), filter(), reduce(), concat(),
and zip()
PHP Reactive Programming Book (Martin Sikora)
Functional Programming
27. Reactive programming is yet another
programming paradigm. It is based around the ability
to easily express data flows and
the automatic propagation of changes.
This reactive concerns with these following:
• Data flows
• Propagation of change
• Easily express data flow
Reactive Programming
PHP Reactive Programming Book (Martin Sikora)
28. Data flows:
In reactive programming, we want to think
about variables as "values that change over
time".
For example, this could be a mouse position,
user click or data coming via WebSockets.
Basically, any event-based system can be
considered a data stream.
Reactive Programming
PHP Reactive Programming Book (Martin Sikora)
29. Propagation of change:
A very nice example is a spreadsheet editor.
If we set the value of a single cell to
to A1 = A2 + A3, this means that every change
to cells A2 and A3 will be propagated to A1.
In programmers' speech, this corresponds to
the Observer Design Pattern where A2 and A3
are observables and A1 is an observer.
Reactive Programming
PHP Reactive Programming Book (Martin Sikora)
30. The first part about data flows and
propagation of change looks like
the Observer Design Pattern with iterables.
Expressing data flows with ease
could be done with functional programming
Reactive Programming
PHP Reactive Programming Book (Martin Sikora)
31. The Observer Pattern (also known as
Publish-Subscribe Pattern) is a behavioral
design pattern which defines a one-to-many
relationship between objects such that,
when one object changes its state,
all dependent objects are notified and
updated automatically.
https://www.sitepoint.com/understanding-the-observer-pattern
Observer Design Pattern
35. The main differences to the observer pattern
are how we think and manipulate with data
streams. In previous examples, we always
worked with arrays as inputs, which are
synchronous, while data streams can be
both synchronous and asynchronous.
Observer Design Pattern
https://en.wikipedia.org/wiki/Reactive_programming
36. Reactive Extensions (ReactiveX or just Rx in short) are
a set of libraries in various languages that make
reactive programming easy even in languages
where concepts of asynchronous and functional
programming are clumsy, such as PHP.
And Reactive programming doesn't equal
Reactive Extensions
Reactive Programming
PHP Reactive Programming Book (Martin Sikora)
37. A Reactive Extension is a library that introduces
certain principles as one of the possible ways to
approach reactive programming.
Very often, when somebody tells you they're using
reactive programming to do something in their
applications, they're in fact talking about a particular
Reactive Extension library in their favorite language
Reactive Extensions
PHP Reactive Programming Book (Martin Sikora)
38. Reactive Extensions were originally made by
Microsoft for .NET and called Rx.NET.
Later, it was ported by Netflix to Java as RxJava.
Now, there are over a dozen supported languages,
the most popular probably being RxJS -
the JavaScript implementation.
And there is RxPHP for PHP and it is a port of RxJS
(https://github.com/ReactiveX/RxPHP)
Reactive Extensions
PHP Reactive Programming Book (Martin Sikora)
39. 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.
And to use it, just require these in composer.json
Introducing RxPHP
PHP Reactive Programming Book (Martin Sikora)
40. When using RxPHP within your own project,
need to set the default scheduler
Introducing RxPHP
PHP Reactive Programming Book (Martin Sikora)
42. We will add Observable and
Operators. 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
at the end of the chain.
Introducing RxPHP
44. Observables are like a push model, where a value
is pushed down the operator chain when it's ready.
This is very important because it's the Observable
that decides when it should emit the next value.
The internal logic of Observables can do whatever
it needs to (for example,
it can run some asynchronous task)
and still remain completely hidden
PHP Reactive Programming Book (Martin Sikora)
Introducing RxPHP
45. Observables can call three different methods
on their observers
onNext:
This method is called when the next item is
ready to be emitted.
Typically said that "an Observable emits
an item”.
PHP Reactive Programming Book (Martin Sikora)
Introducing RxPHP
46. onError:
Notification called when an error has occurred.
This could be any type of error represented by
an instance of the Exception class.
onComplete:
Notification called when there're no more items
to be emitted
PHP Reactive Programming Book (Martin Sikora)
Introducing RxPHP
47. In RxPHP, every operator that takes a callable
as an argument wraps its call internally
with try catch block.
If the callable throws Exception,
then this Exception is sent as onError
notification
PHP Reactive Programming Book (Martin Sikora)
Introducing RxPHP
48. It's important to see that, when an error
occurred, no more items were emitted,
there's also no complete notification.
This is because, when the observer received
an error, it automatically unsubscribed
PHP Reactive Programming Book (Martin Sikora)
Introducing RxPHP
49. Observables:
RxPHP comes with several basic types
of Observables for general usage.
Here are a few that are easy to use:
• ArrayObservable
• RangeObservable
• IteratorObservable
PHP Reactive Programming Book (Martin Sikora)
Components of RxPHP
50. Observers:
Observers are consumers of Observables.
In other words, observers react to
Observables. We've already seen
the CallbackObserver class, which takes
three optional arguments representing
callables for each type of signal”
PHP Reactive Programming Book (Martin Sikora)
Components of RxPHP
51. Singles:
Singles are like Observables
the only difference is that they always emit
just one value.
PHP Reactive Programming Book (Martin Sikora)
Components of RxPHP
52. Subject:
The Subject is a class that acts as
an Observable and observer at the same time.
This means that it can subscribe to
an Observable just like an observer,
and also emit values like an Observable does.
Eventually, it can also emit its own values
independently of its source Observable”
PHP Reactive Programming Book (Martin Sikora)
Components of RxPHP
55. Disposable:
All Rx implementations internally use the
Dispose pattern.
This design decision has two reasons:
• To be able to unsubscribe from an
Observable
• To be able to release all data used by that
Observable
PHP Reactive Programming Book (Martin Sikora)
Components of RxPHP
56. Scheduler:
Observables and operators usually
don't execute their work directly,
but use an instance of the Scheduler class
to decide how and when it should be executed
PHP Reactive Programming Book (Martin Sikora)
Components of RxPHP
57. The core principle of Rx is using various
operators to modify data flow.
Typically, an operator returns another
Observable and therefore allows the
chaining of operator calls
PHP Reactive Programming Book (Martin Sikora)
Operators of RxPHP
58. filter:
It takes values and a predicate function as
input.
Then it evaluates each value with the predicate
and, based on whether it returns true or false,
it adds or skips the value in its response array.
The behavior of the filter() operator is the same
it just works with data flows instead of arrays.
PHP Reactive Programming Book (Martin Sikora)
Operator of RxPHP