Understanding functional programming with PHP

Unlike any object-oriented programming language where everything is represented through an object, functional programming starts thinking everything in terms of functions. OOP and FP are not mutually exclusive. While OOP focuses on code maintainability and reusability by encapsulation and inheritance, functional programming, unlike state-oriented imperative programming, is focused on value-oriented programming, which considers computation as a pure mathematical evaluation and avoids mutability and state modification. When working with OOP, one of the challenges is that the object we have created can bring many extra properties or methods along with it whether or not we are using it in a particular case. Here are the three key characteristics of functional programming:

  • Immutability
  • Pure functions and referential transparency
  • First-class citizen functions
    • Higher order function
    • Function composition (currying)

The concept of immutability tells us that an object will not change after its creation. It will remain the same during its whole life cycle. It has a great advantage as we do not need to revalidate the object whenever we use it. Also, if it requires being mutable, we can create a copy of the object or create a new object with new properties.

So far, in this book, we saw lots of examples of data structures and algorithms using code blocks, loops, and conditions. In general, this is known as imperative programming, where it is expected to define each step of the execution. For example, consider the following code block:

$languages = ["php", "python", "java", "c", "erlang"];

foreach ($languages as $ind => $language) {
$languages[$ind] = ucfirst($language);
}

The preceding code actually sets the first character of each name to upper case. Logically, the code is correct and we have presented it here step-by-step, so that we understand what is going on. However, this can be written as a single line using the functional programming approach, as follows:

$languages = array_map('ucfirst', $languages);

Both of these methods do the same thing, but one is a comparatively smaller code block than the other one. The later one is known as declarative programming. While imperative programming focuses on algorithms and steps, declarative programming focuses on input and output of the function along with recursion (not iteration).

Another important aspect of functional programming is that it is free of any side effects. It is an important feature to have and it ensures that a function will not have any implicit effects anywhere on the input. One of the common examples of functional programming is sorting an array in PHP. Usually, the argument is passed by reference and when we get the sorted array, it actually destroys the initial array. This is an example of side effects in a function.

Before jumping to functional programming with PHP, let's explore some functional programming terms, which we will come across in the following sections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset