Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP Reactive Programming at PHP Confrence Asia ...

Avatar for Dolly Aswin Dolly Aswin
October 01, 2018
440

PHP Reactive Programming at PHP Confrence Asia 2018

The term reactive was very famous recently. Not only did it get trending, but it has started ruling the software development sector with new blog posts articles every day, and presentations, emerging frameworks and libraries, and more.

This is the improvement from my slide before for Medan Tech Day 2018

Avatar for Dolly Aswin

Dolly Aswin

October 01, 2018

Transcript

  1. Dolly Aswin • Start Programming in PHP since 2004 •

    Zend Certified Engineer PHP 5 (2010) • Zend Framework Certified Engineer (2011) • Zend Framework Certified Architect (2015)

  2. 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
  3. 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.
  4. Declarative Declarative programming is a paradigm focused on describing a

    program's logic instead of particular executional steps. (Image Reference: https://xkcd.com/149/)
  5. 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.
  6. Asynchronous Minimize idle time by switching the tasks Creating New

    User Process With Asynchronous Programming Send Welcome Email Give Success Response Create New User
  7. 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
  8. 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.
  9. 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
  10. 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()
  11. 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
  12. Reactive Programming Data Flows In reactive programming, we want to

    think about variables as "values that change over time".
  13. 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
  14. Reactive Programming Easily Express Data Flow The first part about

    data flows and propagation of change looks like the Observer Design Pattern with Iterables.
  15. 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
  16. 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.
  17. 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
  18. 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
  19. 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
  20. RxPHP Components of RxPHP: • Observables • Observers • Singles

    • Subject • Disposable • Scheduler • Operators
  21. RxPHP Observable • Observable emits item • Observer subscribe to

    Observable • Observable notify Data is emitted Error is occured
  22. RxPHP Observable RxPHP comes with several basic types of Observables

    for general usage • ArrayObservable • RangeObservable • IteratorObservable
  23. 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
  24. RxPHP Observers • Observers are consumer of Observables • The

    Constructor takes three optional arguments representing callables for each type of signal onNext() onError() onComplete()
  25. RxPHP Observers Create our own Observer class for reusable to

    test what's going on inside Observable chains
  26. 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:
  27. 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.
  28. 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
  29. 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.
  30. RxPHP Operators • Operators modify data flow • Returns another

    Observables • Allows chaining of Operator calls • There are about 40 operators
  31. 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 10 12 18 20 filter(x => x > 5)
  32. 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 10 12 20 18 20 concat() 14
  33. 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.
  34. 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/
  35. Resources • PHP Reactive Programming Book (Martin Sikora) • Learning

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