0% found this document useful (0 votes)
59 views

Dolly Aswin PHP Conf 2018 - PHP Reactive Programming

The document is a presentation about reactive programming by Dolly Aswin at the PHP Conference Asia in Singapore on September 28, 2018. Dolly has been programming in PHP since 2004 and is a certified Zend Framework engineer and architect. The presentation discusses what reactive programming is, common programming paradigms used in reactive programming like declarative, asynchronous, and functional programming. It also covers key concepts of reactive programming like data flows, propagation of change, and easily expressing data flows. The presentation uses PHP Reactive Extensions (RxPHP) as an example to demonstrate observables, observers, subjects, operators, and other components of reactive programming.

Uploaded by

Dolly Aswin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Dolly Aswin PHP Conf 2018 - PHP Reactive Programming

The document is a presentation about reactive programming by Dolly Aswin at the PHP Conference Asia in Singapore on September 28, 2018. Dolly has been programming in PHP since 2004 and is a certified Zend Framework engineer and architect. The presentation discusses what reactive programming is, common programming paradigms used in reactive programming like declarative, asynchronous, and functional programming. It also covers key concepts of reactive programming like data flows, propagation of change, and easily expressing data flows. The presentation uses PHP Reactive Extensions (RxPHP) as an example to demonstrate observables, observers, subjects, operators, and other components of reactive programming.

Uploaded by

Dolly Aswin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

PHP REACTIVE PROGRAMMING

PHP CONFRENCE ASIA


SINGAPORE

SEPTEMBER 28, 2018

Dolly Aswin
[email protected]
Dolly Aswin
• Start Programming in PHP since 2004
• Zend Certified Engineer PHP 5 (2010)
• Zend Framework Certified Engineer (2011)
• Zend Framework Certified Architect (2015)

What is Reactive Programming
Reactive Programming
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
What is Programming Paradigm
Programming Paradigm
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.
Programming Paradigms Used
• Declarative

• Asynchronous

• Functional
Declarative
Declarative programming is a paradigm focused on describing a
program's logic instead of particular executional steps.

(Image Reference: https://xkcd.com/149/)


Declarative
Example:
SQL
SELECT * FROM `item` WHERE `id` = 1

In SQL, we define what data from what table we want to


query, but the implementation details are completely
hidden for us.
Asynchronous
Minimize idle time by switching the tasks

Send
Welcome Email

Give
Create New User
Success Response

Creating New User Process With Asynchronous Programming


Asynchronous

Send Give
Create New User Welcome Email Success Response

Creating New User Process With Synchronous Programming


Asynchronous != Paralel
Parallelism is a simultaneous execution of multiple
things (they may be related and may not).
In asynchronous, we are dealing with a lot of different
things at once.

Parallelism is doing a lot of things at once. It looks like


the same but these are actually different ideas.
Asynchronous is about structure, while parallelism is
about execution
Functional

(The Image Reference: https://dev.to/navi/why-functional-programming-matters-2o95)


Functional
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.
Functional
Example in Javascript:

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
Functional
In functional programming, this concept of higher-order
function is often used in connection with methods on
collections such as
• map()
• filter()
• reduce()
• concat()
• zip()
Back To Reactive Programming
Reactive Programming

(Image Reference: https://developers.redhat.com/blog/2017/06/30/5-things-to-know-


about-reactive-programming/)
Reactive Programming
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
Data Flows

In reactive programming, we want to think


about variables as "values that change over time".
Reactive Programming
Propagation of change

A very nice example is a spreadsheet editor.


If we set the value of a single cell to C1 = A1 + B1,
every change to cells A1 and B1 will be propagated to C1
Reactive Programming
Easily Express Data Flow

The first part about data flows and propagation


of change looks like the Observer Design Pattern
with Iterables.
Reactive Programming
Observer Design Pattern

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
Reactive Programming
Reactive Extensions:

ReactiveX or just Rx 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.
RxPHP
RxPHP
Output:
RxPHP
Difference Between Observable & Iterating Array

PUSH vs PULL
RxPHP
Observables behave like a data stream
RxPHP
Observable

map()

filter()

Observer
RxPHP
Observable call three methods on it’s Observer

onNext()
called when the next item is ready to be emitted.

onError()
Notification called when an error has occurred

onComplete()
Notification called when there're no more items
to be emitted
RxPHP
In RxPHP, every operator takes a callable as an argument
wraps its call internally. If the callable throws Exception,
then this Exception is sent as onError() notification
RxPHP
Output:

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
RxPHP
Components of RxPHP:
• Observables
• Observers
• Singles
• Subject
• Disposable
• Scheduler
• Operators
RxPHP
Observable
• Observable emits item
• Observer subscribe to Observable
• Observable notify
Data is emitted
Error is occured
RxPHP
Observable
RxPHP comes with several basic types of Observables
for general usage
• ArrayObservable
• RangeObservable
• IteratorObservable
RxPHP
Observable
All built-in Observables in RxPHP can be instantiated easily:
• RxObservable::fromArray()
returns Rx\Observable\ArrayObservable
• RxObservable::range() method
returns Rx\Observable\RangeObservable
• RxObservable::fromIterator() method
returns Rx\Observable\IteratorObservable
RxPHP
Observers
• Observers are consumer of Observables
• The Constructor takes three optional arguments
representing callables for each type of signal
onNext()
onError()
onComplete()
RxPHP
Observers
Create our own Observer class for reusable
to test what's going on inside Observable chains
RxPHP
Singles
Singles are like Observables, the only difference is that they
always emit just one value.
In RxPHP, we don't distinguish any difference between
Observables and Singles, so we can use the Observable::just()
static method:
RxPHP
Output:
RxPHP
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.
RxPHP
Subject as Observable:

Output:
RxPHP
Subject as Observer:

Output:
RxPHP
Subject in the middle of Operator Chain

Output:
RxPHP
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

https://en.wikipedia.org/wiki/Dispose_pattern
RxPHP
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

In most situations in RxPHP, we don't have to even worry


about Schedulers because, if not set differently,
all Observables and operators internally use the
ImmediateScheduler class, which executes
all actions immediately without any further logic.
RxPHP
Scheduler
Applying Event Loop from ReactPHP for Observables
would work in a similar way.
RxPHP
Operators
• Operators modify data flow
• Returns another Observables
• Allows chaining of Operator calls
• There are about 40 operators
RxPHP
filter()
The behavior of the filter() operator is the same,
it just works with data flows instead of arrays.
This means it receives items from its source
(the preceding Observable) and propagates them
to its consequent observer (or chained operator).

5 10 12 20 18

filter(x => x > 5)

10 12 20 18
RxPHP
concat()
This operator is similar with merge(). This operator merges
multiple Observables into one. It internally subscribes to each
input Observable in order, one after another.
This means that, when the first Observable completes, it
subscribes to the next one.”
10 12 20 18

20 14

concat()

10 12 20 18 20 14
Summary
• Using Reactive Programming make our code Concise,
Clear, and Readable.
• Reactive Programming and Reactive eXtensions
provides a development model to tame the asynchronous
beast.
• Combining the power of Reactive Programming and PHP
is the the good approach to create modern PHP Application
• Using reactive programming does not transform our
system into a Reactive System. Because Reactive Systems
are the next level.
Reactive Programming != Reactive System

Reactive System

(The Image Reference: medium.com)

https://developers.redhat.com/blog/2017/06/30/5-things-to-know-about-reactive-
programming/
Resources

• PHP Reactive Programming Book (Martin Sikora)


• Learning Event Drivent PHP With ReactPHP Book
(Sergey Zhuk)
• ReactiveX Website http://reactivex.io

THANK YOU

https://github.com/dollyaswin/reactive-php

You might also like