SlideShare a Scribd company logo
PHP Reactive Programming
By Dolly Aswin
dolly.aswin@gmail.com
Dolly Aswin
• Programming with 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 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
What is 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.
PHP Reactive Programming Book (Martin Sikora)
Programming Paradigm
Programming Paradigm
• Imperative Paradigm
• Declarative Paradigm
• Sequential & Paralel Programming
• Asynchronous Programming
• Functional Programming
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
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
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
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
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
In parallel programming, multiple processes
can be executed concurrently:
PHP Reactive Programming Book (Martin Sikora)
Sequential & Paralel Programming
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
“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
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
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
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
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
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
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
Functional Programming
Example:
Functional Programming
Functional Programming
The following code represents the same example
but uses lstrojny/functional-php library
PHP Reactive Programming Book (Martin Sikora)
Functional Programming
Example In Javascript:
Back To 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
PHP Reactive Programming Book (Martin Sikora)
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)
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)
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)
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
Observable:
Observer Design Pattern
Observer:
Observer Design Pattern
Usage:
Observer Design Pattern
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
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)
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)
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)
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)
When using RxPHP within your own project,
need to set the default scheduler
Introducing RxPHP
PHP Reactive Programming Book (Martin Sikora)
Introducing RxPHP
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
Introducing RxPHP
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
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
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
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
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
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
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
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
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
PHP Reactive Programming Book (Martin Sikora)
Components of RxPHP
Output:
PHP Reactive Programming Book (Martin Sikora)
Components of 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
PHP Reactive Programming Book (Martin Sikora)
Components of 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
PHP Reactive Programming Book (Martin Sikora)
Components of RxPHP
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
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
PHP Reactive Programming Book (Martin Sikora)
Operator of RxPHP
Reactive Programming:
- Declarative Paradigm Programming
- Asynchronous
- Functional Programming
- Observer Design Pattern
- ReactiveX is library for Reactive Programming
- RxPHP consist of: Components and Filter
Summary
- PHP Reactive Programming (Martin Sikora)
- https://github.com/ReactiveX/RxPHP
- https://github.com/reactphp/event-loop
Reference
Thank You
Any update will be published on 

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

More Related Content

PDF
Research paper on python by Rj
PDF
Implementing OpenAPI and GraphQL services with gRPC
PDF
Build your next REST API with gRPC
PPT
PPTX
Programming Paradigm & Languages
PDF
Networked APIs with swift
PPTX
Protocol oriented programming_talk_ppt
PDF
Grooming with Groovy
Research paper on python by Rj
Implementing OpenAPI and GraphQL services with gRPC
Build your next REST API with gRPC
Programming Paradigm & Languages
Networked APIs with swift
Protocol oriented programming_talk_ppt
Grooming with Groovy

What's hot (20)

PPTX
C# 4.0 and .NET 4.0
PDF
F# and SignalR for a FastWeb
PPTX
Intro to java 8
PPTX
Object Oriented Apologetics
DOC
Programming paradigms
PDF
Functional programming with Java 8
PDF
Kotlin & Arrow the functional way
PPTX
Python Lambda Function
PPTX
Python Introduction | JNTUA | R19 | UNIT 1
PDF
Large Scale Text Processing
PDF
Big Data Spain 2017 - Deriving Actionable Insights from High Volume Media St...
PDF
Embracing diversity searching over multiple languages
ODP
PPTX
scope of python
PDF
Kotlin & arrow: the functional way
PDF
The Ring programming language version 1.5.3 book - Part 186 of 194
ODP
PDF
Refactoring to Java 8 (Devoxx UK)
PPTX
Common Programming Paradigms
PDF
My 10 favorite Haxe language features - Francis Bourre - Codemotion Rome 2017
C# 4.0 and .NET 4.0
F# and SignalR for a FastWeb
Intro to java 8
Object Oriented Apologetics
Programming paradigms
Functional programming with Java 8
Kotlin & Arrow the functional way
Python Lambda Function
Python Introduction | JNTUA | R19 | UNIT 1
Large Scale Text Processing
Big Data Spain 2017 - Deriving Actionable Insights from High Volume Media St...
Embracing diversity searching over multiple languages
scope of python
Kotlin & arrow: the functional way
The Ring programming language version 1.5.3 book - Part 186 of 194
Refactoring to Java 8 (Devoxx UK)
Common Programming Paradigms
My 10 favorite Haxe language features - Francis Bourre - Codemotion Rome 2017
Ad

Similar to PHP Reactive Programming at Medan Tech Day 2018 (20)

PDF
FRP with Ractive and RxJS
PDF
Building and deploying LLM applications with Apache Airflow
PPTX
What’s expected in Spring 5
PPTX
Real world functional reactive programming
PDF
Essential Guide To Php For All Levels O Adeolu
PPTX
Modern Frontend Technology
PDF
Programming Reactive Extensions and LINQ 1st Edition Jesse Liberty
PDF
chapter 5 Server-Side Scripting (PHP).pdf
PDF
Programming Reactive Extensions and LINQ 1st Edition Jesse Liberty
PPT
Programming Paradigms
PPTX
Programming paradigms Techniques_part2.pptx
PPTX
Web Dev 21-01-2024.pptx
PDF
Programming Reactive Extensions and LINQ 1st Edition Jesse Liberty
ODP
Phpbasics And Php Framework
PPTX
Prgramming paradigms
PPTX
python programming ppt-230111072927-1c7002a5.pptx
PPTX
chapter Two Server-side Script lang.pptx
PPT
Programming Paradigms
PPTX
program fundamentals using python1 2 3 4.pptx
PPTX
PYTHON PPT.pptx
FRP with Ractive and RxJS
Building and deploying LLM applications with Apache Airflow
What’s expected in Spring 5
Real world functional reactive programming
Essential Guide To Php For All Levels O Adeolu
Modern Frontend Technology
Programming Reactive Extensions and LINQ 1st Edition Jesse Liberty
chapter 5 Server-Side Scripting (PHP).pdf
Programming Reactive Extensions and LINQ 1st Edition Jesse Liberty
Programming Paradigms
Programming paradigms Techniques_part2.pptx
Web Dev 21-01-2024.pptx
Programming Reactive Extensions and LINQ 1st Edition Jesse Liberty
Phpbasics And Php Framework
Prgramming paradigms
python programming ppt-230111072927-1c7002a5.pptx
chapter Two Server-side Script lang.pptx
Programming Paradigms
program fundamentals using python1 2 3 4.pptx
PYTHON PPT.pptx
Ad

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Empathic Computing: Creating Shared Understanding
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Big Data Technologies - Introduction.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPT
Teaching material agriculture food technology
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Approach and Philosophy of On baking technology
Chapter 3 Spatial Domain Image Processing.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
“AI and Expert System Decision Support & Business Intelligence Systems”
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Understanding_Digital_Forensics_Presentation.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Empathic Computing: Creating Shared Understanding
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
MIND Revenue Release Quarter 2 2025 Press Release
Big Data Technologies - Introduction.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
NewMind AI Weekly Chronicles - August'25 Week I
Teaching material agriculture food technology
sap open course for s4hana steps from ECC to s4
Digital-Transformation-Roadmap-for-Companies.pptx
Advanced methodologies resolving dimensionality complications for autism neur...
Approach and Philosophy of On baking technology

PHP Reactive Programming at Medan Tech Day 2018

  • 1. PHP Reactive Programming By Dolly Aswin dolly.aswin@gmail.com
  • 2. Dolly Aswin • Programming with PHP since 2004 • Zend Certified Engineer PHP 5 (2010) • Zend Framework Certified Engineer (2011) • Zend Framework Certified Architect (2015)
  • 3. What is Reactive Programming?
  • 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
  • 7. Programming Paradigm • Imperative Paradigm • Declarative Paradigm • Sequential & Paralel Programming • Asynchronous Programming • Functional Programming
  • 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
  • 24. Functional Programming The following code represents the same example but uses lstrojny/functional-php library
  • 25. PHP Reactive Programming Book (Martin Sikora) Functional Programming Example In Javascript:
  • 26. Back To Reactive 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
  • 53. PHP Reactive Programming Book (Martin Sikora) Components of RxPHP
  • 54. Output: 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
  • 59. PHP Reactive Programming Book (Martin Sikora) Operator of RxPHP
  • 60. Reactive Programming: - Declarative Paradigm Programming - Asynchronous - Functional Programming - Observer Design Pattern - ReactiveX is library for Reactive Programming - RxPHP consist of: Components and Filter Summary
  • 61. - PHP Reactive Programming (Martin Sikora) - https://github.com/ReactiveX/RxPHP - https://github.com/reactphp/event-loop Reference
  • 62. Thank You Any update will be published on 
 • https://github.com/dollyaswin/reactive-php • https://github.com/dollyaswin/functional-php