SlideShare a Scribd company logo
1 of 62
Download to read offline
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

What's hot

C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0C# 4.0 and .NET 4.0
C# 4.0 and .NET 4.0Buu Nguyen
 
F# and SignalR for a FastWeb
F# and SignalR for a FastWebF# and SignalR for a FastWeb
F# and SignalR for a FastWebRiccardo Terrell
 
Intro to java 8
Intro to java 8Intro to java 8
Intro to java 8John Godoi
 
Object Oriented Apologetics
Object Oriented ApologeticsObject Oriented Apologetics
Object Oriented ApologeticsVance Lucas
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8Talha Ocakçı
 
Kotlin & Arrow the functional way
Kotlin & Arrow the functional wayKotlin & Arrow the functional way
Kotlin & Arrow the functional wayThoughtworks
 
Python Lambda Function
Python Lambda FunctionPython Lambda Function
Python Lambda FunctionMd Soyaib
 
Python Introduction | JNTUA | R19 | UNIT 1
Python Introduction | JNTUA | R19 | UNIT 1 Python Introduction | JNTUA | R19 | UNIT 1
Python Introduction | JNTUA | R19 | UNIT 1 FabMinds
 
Large Scale Text Processing
Large Scale Text ProcessingLarge Scale Text Processing
Large Scale Text ProcessingSuneel Marthi
 
Big Data Spain 2017 - Deriving Actionable Insights from High Volume Media St...
Big Data Spain 2017  - Deriving Actionable Insights from High Volume Media St...Big Data Spain 2017  - Deriving Actionable Insights from High Volume Media St...
Big Data Spain 2017 - Deriving Actionable Insights from High Volume Media St...Apache OpenNLP
 
Embracing diversity searching over multiple languages
Embracing diversity  searching over multiple languagesEmbracing diversity  searching over multiple languages
Embracing diversity searching over multiple languagesSuneel Marthi
 
Kotlin & arrow: the functional way
Kotlin & arrow:  the functional wayKotlin & arrow:  the functional way
Kotlin & arrow: the functional waynluaces
 
The Ring programming language version 1.5.3 book - Part 186 of 194
The Ring programming language version 1.5.3 book - Part 186 of 194The Ring programming language version 1.5.3 book - Part 186 of 194
The Ring programming language version 1.5.3 book - Part 186 of 194Mahmoud Samir Fayed
 
Refactoring to Java 8 (Devoxx UK)
Refactoring to Java 8 (Devoxx UK)Refactoring to Java 8 (Devoxx UK)
Refactoring to Java 8 (Devoxx UK)Trisha Gee
 
Common Programming Paradigms
Common Programming ParadigmsCommon Programming Paradigms
Common Programming ParadigmsSuheyl Zafar
 
My 10 favorite Haxe language features - Francis Bourre - Codemotion Rome 2017
My 10 favorite Haxe language features - Francis Bourre - Codemotion Rome 2017My 10 favorite Haxe language features - Francis Bourre - Codemotion Rome 2017
My 10 favorite Haxe language features - Francis Bourre - Codemotion Rome 2017Codemotion
 

What's hot (20)

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

Similar to PHP Reactive Programming Book Explains Paradigms Like Declarative & Functional

Building and deploying LLM applications with Apache Airflow
Building and deploying LLM applications with Apache AirflowBuilding and deploying LLM applications with Apache Airflow
Building and deploying LLM applications with Apache AirflowKaxil Naik
 
What’s expected in Spring 5
What’s expected in Spring 5What’s expected in Spring 5
What’s expected in Spring 5Gal Marder
 
Real world functional reactive programming
Real world functional reactive programmingReal world functional reactive programming
Real world functional reactive programmingEric Polerecky
 
Modern Frontend Technology
Modern Frontend TechnologyModern Frontend Technology
Modern Frontend TechnologyShip Hsu
 
chapter 5 Server-Side Scripting (PHP).pdf
chapter 5 Server-Side Scripting (PHP).pdfchapter 5 Server-Side Scripting (PHP).pdf
chapter 5 Server-Side Scripting (PHP).pdfburasyacob012
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming ParadigmsJaneve George
 
Phpbasics And Php Framework
Phpbasics And Php FrameworkPhpbasics And Php Framework
Phpbasics And Php Frameworkshivas
 
chapter Two Server-side Script lang.pptx
chapter  Two Server-side Script lang.pptxchapter  Two Server-side Script lang.pptx
chapter Two Server-side Script lang.pptxalehegn9
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming ParadigmsDirecti Group
 
Programming paradigm and web programming
Programming paradigm and web programmingProgramming paradigm and web programming
Programming paradigm and web programmingMohammad Kamrul Hasan
 
Listen and look at your PHP code
Listen and look at your PHP codeListen and look at your PHP code
Listen and look at your PHP codeGabriele Santini
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogrammingLuis Atencio
 
Functional programming ruby mty
Functional programming   ruby mtyFunctional programming   ruby mty
Functional programming ruby mtyAdrianGzz2112
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNico Ludwig
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)Arjun Shanka
 

Similar to PHP Reactive Programming Book Explains Paradigms Like Declarative & Functional (20)

FRP with Ractive and RxJS
FRP with Ractive and RxJSFRP with Ractive and RxJS
FRP with Ractive and RxJS
 
Building and deploying LLM applications with Apache Airflow
Building and deploying LLM applications with Apache AirflowBuilding and deploying LLM applications with Apache Airflow
Building and deploying LLM applications with Apache Airflow
 
What’s expected in Spring 5
What’s expected in Spring 5What’s expected in Spring 5
What’s expected in Spring 5
 
Real world functional reactive programming
Real world functional reactive programmingReal world functional reactive programming
Real world functional reactive programming
 
Modern Frontend Technology
Modern Frontend TechnologyModern Frontend Technology
Modern Frontend Technology
 
chapter 5 Server-Side Scripting (PHP).pdf
chapter 5 Server-Side Scripting (PHP).pdfchapter 5 Server-Side Scripting (PHP).pdf
chapter 5 Server-Side Scripting (PHP).pdf
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming Paradigms
 
Web Dev 21-01-2024.pptx
Web Dev 21-01-2024.pptxWeb Dev 21-01-2024.pptx
Web Dev 21-01-2024.pptx
 
Phpbasics And Php Framework
Phpbasics And Php FrameworkPhpbasics And Php Framework
Phpbasics And Php Framework
 
Prgramming paradigms
Prgramming paradigmsPrgramming paradigms
Prgramming paradigms
 
chapter Two Server-side Script lang.pptx
chapter  Two Server-side Script lang.pptxchapter  Two Server-side Script lang.pptx
chapter Two Server-side Script lang.pptx
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming Paradigms
 
PYTHON PPT.pptx
PYTHON PPT.pptxPYTHON PPT.pptx
PYTHON PPT.pptx
 
Programming paradigm and web programming
Programming paradigm and web programmingProgramming paradigm and web programming
Programming paradigm and web programming
 
Listen and look at your PHP code
Listen and look at your PHP codeListen and look at your PHP code
Listen and look at your PHP code
 
PHP Training In Chandigarh
PHP Training In ChandigarhPHP Training In Chandigarh
PHP Training In Chandigarh
 
379008-rc217-functionalprogramming
379008-rc217-functionalprogramming379008-rc217-functionalprogramming
379008-rc217-functionalprogramming
 
Functional programming ruby mty
Functional programming   ruby mtyFunctional programming   ruby mty
Functional programming ruby mty
 
New c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_ivNew c sharp3_features_(linq)_part_iv
New c sharp3_features_(linq)_part_iv
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 

Recently uploaded

Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 

Recently uploaded (20)

Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 

PHP Reactive Programming Book Explains Paradigms Like Declarative & Functional

  • 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