Dolly Aswin PHP Conf 2018 - PHP Reactive Programming
Dolly Aswin PHP Conf 2018 - PHP Reactive Programming
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.
Send
Welcome Email
Give
Create New User
Success Response
Send Give
Create New User Welcome Email Success Response
• Data flows
• Propagation of change
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:
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:
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
5 10 12 20 18
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
https://developers.redhat.com/blog/2017/06/30/5-things-to-know-about-reactive-
programming/
Resources