3.8 Building Packages
Even when writing very small applications, it is recommended to block functions used for certain parts of the applications in separate files. When writing an application used for displaying stock charts, for instance, it can be useful to have separate libraries for functions related to retrieving data and functions for displaying the data. This will help you to reuse code for other applications. In addition, your functions are more consistent because not every application has its own implementation of a block of functions.
Writing reusable code is the first step to writing reliable code. The more often a piece of code is used, the more mature it will be because more testing has been done.
Another important issue is that packages must be independent from the programs using them. In other words, don't access global variables of the main program from a library, or your code might not be reusable any more and the programmer using a library has to know what is going on inside your library. A library will be a black box for those people using it. According to the KISS principle (Keep It Small and Simple) of Unix systems, this is an essential point because other things will be far too complicated.
Try to build small, easy-to-understand, and independent modules with a fixed interface and make sure that the user of a library does not have to know what is going on inside your library.
The next code fragment shows a small example. The target is to write a module that swaps the content of two variables. Let's have a look at the main program first:
<?php include ("lib/transform_data.php"); $a = 23; $b = 37; swapdata_ref($a, $b); echo "a: $a<br>"; echo "b: $b"; ?>
At the beginning of the script a library is included. In the next step two variables are defined. These are the variables you want to swap. Then swapdata_ref is called and the values are passed to the function. After that the variables are displayed so that you can see that they have really been swapped. Up to now, swapdata_ref is a black box, and it is not necessary to know how variables can be swapped because the interface is the function is clearly defined. However, let's have a look at what the function does internally:
<?php function swapdata_ref(&$x, &$y) { $c = $x; $x = $y; $y = $c; } ?>
A reference is passed to the function and the values are swapped using a temporary variable called $c. As you can see, the function has no return values. Because a reference is passed to the swapdata_ref, the function is able to access the variables directly (using the internal addresses of the values) without having to access the variables using global variables. This way of solving the problem is very comfortable because swapdata_ref does not have to know how the variables in the function calling the swap function are calledthe addresses of the values that have to be processed are assigned to their new names when the function is called.
When executing the script, you will see that the variables have been swapped:
a: 37 b: 23
Like many other things, references are borrowed from C and are a powerful feature because it would be far too uncomfortable to work with global variables or passing huge amounts of data to a function.