# Two Ways to Develop WordPress Plugins: Functional Programming

This part two of a series looking at two different programming styles (sometimes called programming paradigms) that you can use when writing WordPress plug-ins. In part one Tom McFarlin covered object-oriented programming. In this part we'll be looking at functional programming.

Because the experience level of readers varies, we’re going to be talking about programming at a high-level, so if you’re a beginner, then you should have no problem following along. If, however, you’re a more experienced developer, then you may find more useful information later in the article.

## What Is Functional Programming?

Functional programming is probably the style with which you are most familiar - and almost universally - is the style used in the various WordPress code snippet websites floating around the internet. For this reason it can sometimes be viewed as 'entry level' programming: the style employed by beginners until they've learnt to master object-oriented programming. This is incredibly misleading, because while functional programming is much simpler, it is not in itself inferior.

Functional programming emphasises the evaluation of functions, and avoids the notion of states or objects as opposed to object-oriented programming which encourages thinking about code as acting on object(s), using methods to change those objects, or interact with them. Let's look at a very simple example comparing the two styles:

 1 2  // Functional method  3  function add_two( $n ) {  4  return$n +2;  5  }  6 7  $a = 2;  8 $b = add_two( $a ); //$b = 4;  9 10 11  // Object oriented method  12  class Number {  13  var $value = 0;  14 15  function __construct($a ) {  16  $this->value =$a;  17  }  18 19  function add_two() {  20  $this->value =$this->value +2;  21  }  22  }  23 24  $a = new Number( 2 );  25  echo$a->value; //Prints 2  26  $a->add_two();  27  echo$a->value; //Prints 4 

This very simple example illustrates the fundamental difference in style of the two paradigms: functional programming focuses on passing arguments to, and receiving values from functions. There are no 'objects' that are being acted on, only parameters and return values. Conversely, the object-oriented approach assigns an object various properties (in our case a 'value') and methods act on those properties.

## Functions: The Basics

Defining functions is very simple:

 1 2  function add( $number,$number2 = 1 ) {  3  // Perform code acting on passed variables  4  $sum =$number + $number2;  5 6  // Optional, if needed you can return a value  7  return$sum;  8  } 

Once the function is declared it can be used anywhere in your plug-in - in other words it has global scope.

 1 2  $a = 4;  3 $b = 7;  4  echo add( $a,$b ); // Prints 11 
Functions must have unique names. Redeclaring a function will throw an error. Since your code will be running alongside other plug-ins, themes and WordPress itself you should never use generic names. Instead you should prefix your function names with something unique (like the name of your plug-in).

You may have noticed that in the definition of add, the second argument is set equal to 1. This sets a default value for $number2 (in this case, 1), and by doing so makes the argument optional. If the argument is not supplied, the value is taken to be the default value:  1 2  echo add( 4 ); // Prints 5  3  echo add( 4, 1 ); // Prints 5  On the other hand, no default value is provided for the first value, so omitting that argument will throw an error  1 2  echo add(); // Throws an error as$number is not defined 

You can also have a variable number of arguments. Inside the function we can use func_num_args() to get the number of arguments received, while func_get_arg() allows you access a particular passed variable, indexed from 0.

 1 2 function sum() {  3  // Get the number of arguments given to sum()  4  $number_args = func_num_args();  5 6 $sum = 0;  7 8  if ( ! $number_args )  9  return$sum;  10 11  for ( $i = 0;$i < $number_args;$i++ ) {  12  $sum += func_get_arg($i );  13  }  14 15  return $sum;  16 }  17 18 echo sum( 1, 2, 3, 4 ); //Prints 10  19 echo sum( 1, 2 ); //Prints 3  20 echo sum(); //Prints 0  The above can be used in object methods too. Finally, by declaring a variable as global you can access the variable from inside a function.  1 2 $a = 'Hello';  3  $b = 'World';  4  function hello_world() {  5  // This is necessary to access$a and $b  6  // declared outside of the function scope.  7  global$a, $b;  8 $b = $a . ' ' .$b;  9  }  10  hello_world();  11  echo $b; // Prints 'Hello World'  Using globals is generally discouraged. Particularly since two plug-ins using the same name for a global variable can cause either one or both plug-ins to break. If you must use a global variable, again ensure it's unique by prefixing with your plug-in's name. ## Why Use Functional Programming? Deciding which programming style to use comes down to judgement - and yes - personal preference. It is not more right or wrong to use functional over object-oriented programming, but more often than not there is one style which is better suited for what you're trying to achieve. Sometimes object oriented programming simply isn't necessary, and only over complicates matters, or introduces superfluous code. An example might be the various 'utility' functions WordPress provides. These are generic functions which serve to perform a particular purpose. For example wp_trim_words($text, $num_words ) simply trims the given string to a certain size (in words). It wouldn't add anything to define wp_trim_words() instead as a method belonging to some object, and would result in uglier code. With functional programming it takes one line. One advantage of functional programming, particularly for beginners, is its simplicity. You do not have to worry about static, private or protected functions - they are all global. Neither does the notion of static variables exist. At the very basic level your function returns an output derived from what you've given it. For example, get_the_title( 7 ) will return the title for the post with ID 7. Another advantage of functional programming is that functions are accessible globally. With object-oriented programs, in order to act on a specific object, you need to pass around that object. This can sometimes be tricky. To illustrate this let's take an example from part one:  1 2 class DemoPlugin {  3  public function __construct() {  4  add_action( 'wp_enqueue_scripts', array($this, 'register_plugin_scripts' ) );  5  }  6 7  public function register_plugin_scripts() {  8  // Register plugin scripts  9  }  10 }  11 $demo_plugin = new DemoPlugin();  When WordPress stores the register_plugin_scripts() method so that it can be called when the wp_enqueue_scripts action is triggered it does so by referencing not only the method, but also the object $demo_plugin. This is because the same method for different instances of an object are considered different methods - that is, $demo_plugin->register_plugin_scripts() and $copy_of_demo_plugin->register_plugin_scripts() are not the same. This may seem odd - but methods can behave differently for different instances of the same class, so we need to reference both method and instance.

But why does this matter? It makes it very difficult for a third party plug-in or theme to unhook that method, since to do so they would need to call:

 1 2  remove_action( 'wp_enqueue_scripts', array( $demo_plugin, 'register_plugin_scripts' ) );  But in general they won't have access to the $demo_plugin variable. (Note: if the method is declared static, then you can get around this).

## Object-Oriented and Functional Programming in WordPress

Of course object-oriented programming has its advantages, as discussed in part one. As Tom also mentioned, it is unavoidable when using WordPress' widget API. Another common example is WP_Query(). Here an object oriented approach is clearly the best: you have an object (in this case a query), which has various properties (i.e. search criteria, pagination information, matching results) and you want to act on that query (parse it, generate and sanitise the corresponding SQL, and return the results).

WP_Query() demonstrates how powerful object-oriented programming can be when used correctly. After initiating the query:

 1 2 $the_query = new WP_Query( array(...) );  Not only can you access the results, but other information too such as, pagination values: how many pages of results there are, which page is being viewed, the total number of results, and the 'type' of query, e.g. $the_query->is_search(), $the_query->is_single() etc. There is also the entire 'loop' infrastructure;  1 2  if ($the_query->have_posts() ) {  3  echo '
';  4  while( $the_query->have_posts() ):$the_query->the_post();  5  // The Loop  6  echo '
• ' . get_the_title( $the_post->ID ) . ' • ';  7  endwhile;  8  echo ' ';  9  }  10  wp_reset_postdata();  Which hides all the internal juggling of results and globals behind a human-friendly API. So what about get_posts()? This just serves as a wrapper for WP_Query(), and simply returns an array of posts matching the query. As such, you don't get the 'bells and whistles' of WP_Query(), but it is slightly more efficient. So whether you should use get_posts() or WP_Query() depends on your use case (for example, whether you require pagination or not), but it's also down to personal preference.  1 2 $results = get_posts( array( ... ) );  3 4  if ( $results ) {  5  echo ' ';  6  foreach($results as $the_post ) {  7  echo ' • ' . get_the_title($the_post->ID ) . '
• ';  8  }  9  echo '
';  10  } 

## Summary

Hopefully these two articles have helped highlight the advantages and disadvantages of these programming styles. The take away point is that there is no right and wrong here, and each programmer will have their own personal preference. But some contexts lend themselves more readily to a certain programming style - and as such you should expect your plug-in to contain a mixture of both.