If you're new here, you may want to subscribe to my RSS feed. So that you can read the latest updates about Web2.0 tools, Making Money Online, Tips in SEO, Ajax and many more. Thanks for visiting ProgramimiCOM!
There are many reasons for redirecting a user to another page. Perhaps the page has moved or perhaps they are trying to access a page they shouldn’t have access to. Or maybe you want to put in an interstitial ad (those full page ads displayed before taking you to the actual page you want to go to).
There are also many ways of implementing a redirect, each with advantages and disadvantages appropriate for different scenarios.
HTML Redirect/META refresh
This is probably one of the most widely used since it is easy to implement. Using a <meta> tag, once a page is loaded, users can be redirected to another page after a certain amount of time. All those “you will be redirected in 5 seconds” pages use a <meta> refresh tag to accomplish this.
The code goes in the <head></head> section of your HTML and looks like the following:
<meta type=”http-equiv” content=”7;url=http://www.programimi.com” />
The number before the semi-colon indicates how many seconds the browser should wait until redirecting the user to the page specified. If an URI is not specified, the page will refresh itself after the set amount of seconds — useful for continuously updating a page without having to use JavaScript.
The PHP Redirect (and other languages)
Using a server-side language, you can invisibly redirect the user to another page. For example, this can be useful if they try to access a secure page without proper authorization and you wish to redirect them to a login page.
The syntax varies with language but here is the PHP code:
<?php
header(”Location: http://www.programimi.com/”);
?>
This modifies the header to send the user to a desired page. Note that headers can only be modified if no output has been sent yet.
.htaccess Redirect
Using .htaccess to redirect has several purposes. Similar to forcing www, you can use mod_rewrite to send users to a different destination. The code is simple — redirect all page requests to a new domain.
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
The advantage to this method is that it will maintain the page URI the user was trying to access. For example, if they navigate to http://www.olddomain.com/contact.php, they will be redirected to http://www.newdomain.com/contact.php instead of just the domain root.
Moved Permanently - 301 Redirect
Also, note the R=301 option at the end of the .htaccess code above. This 301 code is to let search engines know that the domain has been moved permanently and all old links should be updated to the new domain.
A 301 Redirect can also be achieved in PHP by adding this line prior to the redirection line:
header( “HTTP/1.1 301 Moved Permanently” );
If you’re into SEO (search engine optimization), listing the old domain as 301 is very important.
JavaScript?
There’s actually another method I left out and that is redirecting with JavaScript. I left it out because I can’t think of a situation where it would be better suited than the other methods described above. That, and there are more compatibility issues with JavaScript.
Know of any other ways of redirecting?
Peter B. was an out-of-work PHP developer looking for contract work in early 2005. A recruiter he’d worked with in the past emailed him some information regarding a possible position. Reading the job description, Peter thought he’d be a good fit, so he submitted his resume and got a response via email a few days later.
The hiring manager described their typical process; Peter would have to answer a screening question to determine his skill level, and if his answer was satisfactory, they’d schedule a face-to-face interview. With a little trepidation, Peter said he was ready for the question. He was concerned that it could be about a complex topic that he wasn’t very familiar with. A few hours later, an email arrived with the subject “SCREENING QUESTION,” flagged with high importance.
His mouse hovering over the email, he expected to open it and have to answer “on a PB349 microprocessor, if memory address 0xa9f00c contains a MOV instruction to memory address 0×8ad9da, what is the magnetic force dispensed by a 64KB memory module for the next 600 instructions? You have thirty seconds.”
Peter took a deep breath and clicked on the email. Here is the exact question he was asked: “Describe what concatenation is, how it applies to PHP, and how you’ve used it in the past.”
Peter was surprised. It was a question that anyone with any basic exposure to any modern programming language could answer. It would’ve been harder to describe what multiplication was and how he’d used it in the past.
Still, he wanted to show that he understood the concept, so he played along. He typed up a detailed response.
Concatenation is the process to sequentially join multiple pieces of data, usually literal strings with non-string-literal data (most commonly, variables or other literals). The concatenation operator varies from language to language. Javascript, for example, overloads the plus-sign (+) as it is both the concatenation operator as well as the arithmetic addition operator. PHP uses the period (.) as the concatenation operator.
String concatenation is often used in PHP to build a string of HTML for output to the client (browser). This is common in prodecural-based PHP code. However, I should note that oftentimes, that using concatenation for HTML generation is inefficient or can be better served by some other design pattern - particulary if the developer is using concatention during an echo operation (in this case, comma-separating the tokens is faster).
Another common use for string concatenation is the generation of dynamic SQL queries. For example, if I had a CMS that was to pull all articles written by a certain user, the code might look something like this
<?php
$sql = “SELECT article_id, article_body FROM Articles WHERE author_id = ” . $User->getID() . ” ORDER BY article_date DESC”;
?>As you can see, the above code combines three tokens to generate a complete SQL query.
- A SQL fragment
- The user’s ID as pulled from a custom User object
- A SQL fragment
SQL queries are rarely generated without some sort of dynamic data to alter their structure, so this is a very common task that I’ve used in just about every web application I’ve written. Some other simple examples include cookie generation, error message generation, email headers, and dynamic URL construction.
I hope this sufficiently explains concatenation in general, how it relates to PHP, and my experience with using this basic operator.
Peter sent the email and got a phone call a few days later.
Peter: Hello?
Lisa: Hi, Peter? This is Lisa from Concatcorp.
Peter: Oh, hi! Good to hear from you! I hope you have news about the job.
Lisa: Well, yes, but…
Peter: Yes?
Lisa: We’re going to offer the position to another candidate.
Peter: I see… may I ask why? I thought I did a pretty thorough job answering the screening question…
Lisa: Well, that’s just it. The problem is that they think your answer was too good. They think you plagiarized it. I’m sorry.
It was then that Peter realized he was probably better off without that job.
23 Sep
Posted by ProCOM
on September 23, 2007 – 10:19 pm - 276 views
In the first installment of my series on SPL, we talked about Exceptions. If you have not read it and are not yet familiar with how exceptions work in PHP 5, please do so here. This article assumes knowledge of the Exception class. This installment will cover the new Array object, introduce Iterators and the ArrayIterator, and discuss some practical examples of their usage.
An iterator is, as the name implies, an object that traverses the contents of another object. The object may be a simple array data type or it may be a complex class, but by using iterators, we can standardize the way we traverse objects. The more important concept, however, is providing a way to traverse an object without exposing its internal data structure. In addition to providing a standard interface for object traversal and keeping the internal structure of the object being traversed private, it is common to iterate an object using a filter or to have multiple pending traversals on a single object. Using an iterator allows us to do all this.
The big picture objective behind using special objects such as Iterators is to achieve what is called loose coupling. Loose coupling is present when the internal behavior of objects can change without affecting the objects that interact with them. It also means that private class data is kept private and is not directly accessible to outside objects. This allows classes to store internal data in whatever way they like without forcing other objects to make assumptions about data storage when attempting to directly access class data. The new features in PHP 5 and the introduction of the SPL allow PHP developers to take advantage of these concepts in ways that were previously not possible. To get an overview of the new features in PHP 5 if you are not already familiar with them, have a look at my article: “What’s New in PHP 5″.
We will discuss the new ArrayObject class as a precursor to our discussion on iterators, as the ArrayObject class serves as an excellent example for simple iteration routines.
The Array Object
Programmers familiar with Java and .NET will be at home with using the new ArrayObject class. The ArrayObject class has a definition like this:
class ArrayObject implements IteratorAggregate,
ArrayAccess, Countable {
public function __construct($array);
public function append($value);
public function count();
public function getArrayCopy();
public function getIterator();
public function offsetExists($index);
public function offsetGet($index);
public function offsetSet($index, $newval);
public function offsetUnset($index);
}
As you see in the declaration, the ArrayObject class implements three interfaces. The Countable interface requires the public method count() to be defined and allows the built in PHP count() function to be used on the object. The ArrayAccess interface is used to override array access of objects. For example, the offsetGet($index) method is used in place of the normal array style: $array[$index]. This interfaces calls for the public methods offsetExists(), offsetGet(), offsetSet() and offsetUnset(). The IteratorAggregate interface requires the public method getIterator(), which allows the calling code to use an external iterator for object traversal.
It is worth noting that the IteratorAggregate interface implements the Traversable abstract base interface, which is used to detect if a class is traversable using foreach. This interface must be implemented by either Iterator (an iterator object) or IteratorAggregate (an object to be iterated).
Let’s take a look at some sample code:
$myArray = new ArrayObject();
$myArray->append('a');
$myArray->append('b');
$myArray->append('c');
echo 'First Element: '.$myArray->offsetGet(0).'<br />';
echo 'Second Element: '.$myArray->offsetGet(1).'<br />';
echo 'Third Element: '.$myArray->offsetGet(2).'<br />';
echo 'Number of Elements: '.$myArray->count().'<br />';
$myArray->offsetUnset(0);
$myArray->offsetSet(1, 'a');
echo 'First Element: '.$myArray->offsetGet(0).'<br />';
echo 'Second Element: '.$myArray->offsetGet(1).'<br />';
echo 'Third Element: '.$myArray->offsetGet(2).'<br />';
echo 'Number of Elements: '.$myArray->count().'<br />';
The first line creates the ArrayObject object and the next three lines store the values “a”, “b” and “c” in it. The next four lines demonstrate using the offsetGet() method and the count() method to retrieve values at a specified offset and to return the number of elements in the array, respectively. Next we unset the first index value by calling offsetUnset() and then change the value of the second index to “a”. We then repeat the process of outputting the values in the array and the number of elements in the array. This script will output the following:
First Element: a Second Element: b Third Element: c Number of Elements: 3 First Element: Second Element: a Third Element: c Number of Elements: 2
Note that after calling unset, the index at position 0 still exists but has no value and is not included when calling count(). This seems like a counterintuitive behavior. It seems that the expected result would have been to completely remove the element - index and all - from the array and shift the other elements down. The term unset implies that after calling the method, the value is no longer set at all - meaning it is not set to NULL, 0, ” or anything else, and the index does not exist in the array. I would be interested to hear readers’ thoughts on this, as I see it as poor implementation.
Simple Array Iterators
The following code snippet shows a simple example of creating an ArrayObject object and an ArrayIterator object, then traversing the iterator using the three most common control structures for iteration - foreach, for and while.
$myArray = new ArrayObject();
$myArray->append('a');
$myArray->append('b');
$myArray->append('c');
$i = $myArray->getIterator();
foreach ($i as $item) {
print $item.'<br />';
}
$i->rewind();
for (; $i->valid(); $i->next()) {
print $i->current().'<br />';
}
$i->rewind();
while ($i->valid()) {
print $i->current().'<br />';
$i->next();
}
The first line of this example creates an ArrayObject object and the next three lines call the append() method to add “a”, “b” and “c” to the array. The fifth line gets an ArrayIterator object from the ArrayObject by calling the getIterator() method. Once we have the iterator, we are free to traverse the object in a number of different ways. The first method shown uses the foreach() control structure. Note that after traversing the iterator, we must reset it to its initial position with the rewind() method.
The second method demonstrates using an iterator with a for loop. Note that we do not need any initialization condition in the loop, but we could move the call to rewind() after the previous iteration to the for loop if desired. The condition section of the loop calls the valid() method of our iterator which returns true if more elements of the array exist after our current position and false if not. For our change section, we call the next() method which advances the iterator to the next element it contains.
The last method uses a while loop. Our condition, like the for loop above, uses the valid() method. In this example, however, we call next() after working with our data.
Directory Iteration
PHP developers are accustomed to using a special variable type - resource handles. Resource handles are used by PHP to identify external resources such as open files, directories and database connections. The introduction of the DirectoryIterator class allows developers to traverse a directory through a standard iterator interface, providing a high level of abstraction between the data source (the directory) and application components that use the data source. Previously a developer would perform a simple directory iteration like this :
$dh = opendir('images/');
if (!$dh)
die('Unable to open directory');
while (($resource = readdir($dh)) !== false) {
print $resource.'<br />';
}
This rudimentary example would traverse a directory and output all the files and directories that it contains. The problem with this method of directory iteration is that the calling application has to know the data source is a directory. The following example demonstrates the use of a directory iterator. After the example, I will explain how this could be used in a non-specific context by the calling object.
try {
$iter = new DirectoryIterator('images/');
while ($iter->valid()) {
print $iter->current().'<br />';
$iter->next();
}
}
catch (Exception $e) {
print_r($e);
}
This code should look familiar - it is identical to the array example provided above, only with a different Iterator class. You are probably wondering at this point how we have allowed our calling code to stay unaware of the actual data source. Consider this: we have an application that allows a common display component to output the contents of any iterator. The calling application sends it the right iterator object and it outputs the data. This is a highly simplified example, but it is sufficient to demonstrate intent.
try {
TraverseIterator(new DirectoryIterator('images/'));
TraverseIterator(new ArrayIterator
(new ArrayObject(array('a', 'b', 'c'))));
}
catch (Exception $e) {
print_r($e);
}
function TraverseIterator(Iterator $iter) {
while ($iter->valid()) {
print $iter->current().'<br />';
$iter->next();
}
}
In this example we define a function called TraverseIterator that takes an Iterator object as its only argument. If an object that is not a child class of Iterator is passed to the function, an Exception will be thrown. Our application calls the function twice - once with a DirectoryIterator and once with an ArrayIterator. There are certainly more practical applications of this technique but this demonstrates the basic idea.
Going further, a developer could create some other object that handles creation of iterators. An object whos job is to create other objects is known as a Factory. If we create a class, IteratorFactory, we can use this to create the iterator needed and the calling application would not have to know what type of iterator to create - it would only need the object to traverse and the type of object. A detailed example of this is a subject for another article, but the concept is worth touching on here.
Recursive Iteration
In our example above we used an example that traversed a directory and displayed its contents. This is fine if you are simply checking the contents of a single directory, but what if you need to traverse an entire directory tree? Perhaps you may be searching for a file or simply preparing formatted output. The example below demonstrates a simple way of displaying a directory tree for a given path.
try {
$iter = new DirectoryIterator('images/');
print WalkDirectory($iter);
}
catch (Exception $e) {
print_r($e);
}
function WalkDirectory(DirectoryIterator $iter, $depth = 0) {
$return = str_repeat(' ',
($depth * 5)).$iter->getPathName().'<br />';
while ($iter->valid()) {
$node = $iter->current();
if ($node->isDir() && $node->isReadable() && !$node->isDot()) {
$return .= WalkDirectory
(new DirectoryIterator($node->getPathname()), $depth + 1);
}
elseif ($node->isFile())
$return .= str_repeat
(' ', ((1 + $depth) * 5)).$node->getFilename().'<br />';
$iter->next();
}
return $return;
}
First let us discuss the WalkDirectory function. This function takes a DirectoryIterator object as its first parameter and is required. The second parameter, depth, should not be specified by the calling application, as it is used solely to track the depth of the current traversal in the directory tree. The first line of the function grabs an appropriate amount of padding for the current depth level (we use $depth to determine how far to indent a file or directory in our output) and the current directory path name with the getPathName() method.
The next block of code beginning with the while loop is where we begin to traverse the DirectoryIterator. First we put the node from our DirectoryIterator object into a variable $node. We then test if the variable is a non-dot directory (that is, not “.” or “..”). If the current node is a directory, we append the return value of another call to WalkDirectory to our current return value. This is where the recursion we mentioned before steps in. Recursion is a topic unto itself but in short, recursion is a function calling itself with a known set of operating conditions until a desired target condition is reached. For a higher quality introduction to recursion than could be provided in this article, read “Solving Problems with Recursion” by Mohamed Saad.
If the node is not a directory then it is a file, and we simply append the appropriate amount of padding and the name of the file. The following line advances the iterator one step. Finally, on the last line, we return our return value. If we output the return value, we will see a primitive tree view of the directory we traversed.
The makers of PHP considered this scenario, though, and provided a better way of of recursively iterating structures that require recursion for complete traversal - such as directories, multidimensional arrays and any object with nested objects that use the same type of Iterator.
Conclusion
In short, the introduction of the various interfaces related to Iterators allows PHP developers to strongly implement a sort of regimented object traversal that truly prevents outside code from seeing inside classes’ private data. The new features in PHP 5 and the introduction of the SPL are great steps toward improving developers’ abilities to develop reusable, loosely coupled classes. Keeping private data private is an important part of this and the ArrayObject, Iterator interfaces and classes come built in to assist the task of creating strong code.
As was mentioned on the previous page during our discussion of recursive iteration, there is another group of Iterators that specifically provide a method for recursive object traversal. These will be the subject of the third and final installment in this series on SPL.
—
by David Fells
Recursion is a way to solve a problem by…reducing it to the same problem. What? It may be counterintuitive, but many turn-based games (including chess) use exactly this technique to make a computer player “think.” Mohamed Saad explains the concept, along with when (and when not) to use recursion in your programming. Check out the Connect4 example!Recursion: Solving problems the recursive way
In this article, I will introduce the concept of recursion, one of the most amazing programming constructs, as you will see. Recursion is quite counter-intuitive, and you will certainly find it weird at first, but once grasped, it becomes a seriously powerful weapon in your programming arsenal. Let’s start with the definition.
What is Recursion?
Recursion is a totally different method of solving problems. Conventional problem solving methods consists of decomposing the solution into steps, and executing each step in turn. Well…enter recursion. The recursive way of solving a problem is to reduce the problem into another problem that is exactly of the same type, and solving the new one instead. And how do you solve the new one? Guess what? You just reduce it into another problem of exactly the same type, and so on, and so on, and so on…
Ok, before it gets too weird, let’s see an example of a real-life problem, and how we can use recursion to solve it.
Real Life Example
Let’s assume you have moved to a new apartment. You are exploring the neighborhood, and you suddenly discover that you don’t know your house number. You looked at the building, didn’t find a number there. You knocked on a random apartment, and asked, “what is the number of this building,” and the guy who opened the door smiled and said, “I don’t know the number of this building, but if you look at the number of the building next to us and add 1, you will know our building number.”
Now, let’s pause for a moment. The problem you are trying to solve is “knowing the number of your house.” Let’s look at the proposed solution to this problem. What was the suggestion? You were asked to look at the number of the building next to you and add 1. Your problem was thus reduced into a problem of exactly the same nature! At first your problem was “to find the number of a house,” and your problem now became “finding the number of a house.”

Fig 1. You never knew knowing your house number could get you into so much trouble, did you?
The Plot Thickens
To confuse things even more, let’s say you visited the house next to you, but you still found no number. You knocked on the door, and asked about the house number. Guess what they said to you? “We don’t know, but look at the house next to us, add 1, and you will know our house number.”
Déjà vu anyone? This game could take a while. You should repeat this again and again, till you find a house with a number you know. You add 1, and know the number of the one next to it, and so on, until you reach your house.
Notice that in every step, your problem was always reduced to exactly the same kind of problem (knowing a house number). This is the heart of recursion. We have just made our first recursive solution to a problem.
Before we go any further, I want to make two very important notes. These notes are going to be extremely useful when we start dealing with recursion in programming.
Note 1: In every step of solving a recursive problem, the problem always reduces to a problem of exactly the same nature.
This one is obvious, but now look at number 2.
Note 2: At a certain point, you should just solve the problem you have immediately rather than reducing it any further.
This note is extremely important. Let’s return back to our house numbers problem. If you just keep asking and you are always asked to see the next house, you will spend your entire lifetime looking at buildings! At a certain point, you have to find a house with a number that you know. At this point, the recursion stops, and you start to solve all the problems you left open.
Let me stress this again: you can’t keep reducing your problem into a similar problem forever, or else you are never going to stop. At a certain point, you have to just stop and solve the problem at hand. This point is sometimes called the stopping condition.
Now, I can already hear you screaming, “What does this have to do with programming?!” Well, a lot, actually. When you are writing a program, you are solving a problem (or a set of problems). If you write your solution to the problem in a recursive way, what will it look like? Basically the function you write to solve the problem is going to eventually call itself. What for? This comes from our definition of recursion. We reduce a problem to a problem of exactly the same nature. This is why the function calls itself to solve the new instance of the problem…
Sounds complicated? Don’t worry. Let’s look at our first programming example, a really simple one.
First Programming Example
As promised, this one is going to be easy. We will take a well known problem, and see how we can solve it recursively. The problem is searching an array. You are given an array, and you want to search for a certain value inside the array. The non-recursive approach to this problem would be to iterate through the array looking for the value we search for. When it is found, we stop and report success, and if not, we report failure.
Now, let’s think recursively. We want to reduce the problem of searching an array into a problem of…well…searching an array. After all, this is how recursion works.
Think of this approach. To search an array whose length is n for the value of v, do the following.
If the element is in the very last position, report as success
If not, search the whole array (except for the last position), for the element we are looking for.
You see, we actually managed to reduce the problem from searching an array into searching an array. Are we forgetting anything? Of course we are: the stopping condition! When should this stop? Luckily this is simple–when you are faced with an empty array, report failure.
Now look at the code to actually implement this…
class List
{
int[] data=new int[100];
public boolean search(int v, int n)
{//v is the value we search for, n is length of array to search
if(n==0) return false;
if(data[n-1]==v) return true;
return search(v,n-1);
}
}
Take a closer look at this code. I have removed everything, except for the relevant parts. Did you notice that there aren’t any loops at all? That’s right, we made a search over all the elements of the array without having any kind of loops in the code. Just a couple of if statements, and a return statement! How does it work?
It is a little tricky. First you call search(v,100). This function checks the last element of the array, and if that’s the one we want, it returns true. If not, it calls search(v,99). The newly called function checks the position before the last one, and if the element is not found there it calls search(v,98), and so on. If the element is not in the array, the function search() will keep calling itself till it reaches search(v,0), which will return false. If the element is found, the function that finds it will return true.
If you think this is complicated, don’t worry. Recursion looks complicated the first time you see it. I mentioned that recursion is quite counter-intuitive. The human brain can never solve problems recursively. Our brains can only solve problems by thinking of the steps necessary, and doing them one by one. Only computers are comfortable with recursion. Anyway, let’s get back to our discussion.
I can hear the skeptics complaining, “I can’t see any benefits from using recursion. We solved a simple problem by writing a really tricky piece of code. We could have just lived with a small loop to do the search, and ignore recursion completely.” Patience people; I can’t say everything at the same time! Let’s head into our second example.
The Flood Fill Algorithm
Now, let’s turn our attention to a totally different problem. We are working on a 2D drawing package, and we want to make a bucket fill tool similar to that found in Microsoft Paint. The user selects a color, clicks on a point, and the color spreads to fill the area with this color. Have a look at this figure.

Fig 2. We need to write a program to flood fill a shape
We want to write a function to implement this feature. We are given a 2 dimensional array of colors, a point to start painting from, and a color. We should fill the fill area with the color as mentioned above.
There is no easy non-recursive solution to this problem, but we can solve this recursively in just 8 lines of code. How? The idea is simply to make a flood fill from a point do the following:
Now, you see, the whole solution is just 5 steps, and 4 of them are recursive calls. It is important to understand how this actually works.
class Canvas
{
byte board[][]=new byte[100][100];
public void Fill(int x,int y,byte c)
{
if(board[x][y]==c) return;
board[x][y]=c;
if(x<99&&board[x+1][y]!=c)
Fill(x+1,y,c);
if(x>0&&board[x-1][y]!=c)
Fill(x-1,y,c);
if(y<99&&board[x][y+1]!=c)
Fill(x,y+1,c);
if(y>0&&board[x][y-1]!=c)
Fill(x,y-1,c);
}
}
I have tried to make the code simpler by fixing width and height. But, of course, it is easy to generalize the code to different sizes and color formats.
How it Works
How does this code work? Looks too small to be true? Well, it isn’t. It works like this: you start at a point, if it is already colored you do nothing. This condition prevents color from flowing out.
Next, you turn your attention to the 4 neighboring pixels. If any one of them is not colored, start a flood fill from that point too. And guess what, when you start a flood fill from each of those points, they will start to fill the neighboring pixels, and so on, and so on… This is the magic of recursion.
Take a look at this figure. It shows the first 3 steps of how flood fill works. This figure is not actually 100% accurate in terms of the sequence of pixels being filled, but I hope it illustrates the idea.

Fig 3. Starting at the red point, the program fills the point, and then calls itself to fill the 4 surrounding points, and then from each of the surrounding point, calls itself to fill the 4 surrounding each one of them and so on (Note: the function calls will not occur concurrently as shown here. This is just for illustration purpose. In reality. They will be called one after the other. The sequence of coloring pixels will be different)
Now you see how it works. We have created a really nice flood fill algorithm that can fill really complicated shapes with a minimum amount of effort.
I’ll let you in on a little secret. The code we have just written is bad in its memory usage. Really bad. Why? The same function is called again for every pixel. Each function call takes a specified amount of memory that is released when the function is finished. And so, by the time we reach the last pixel to fill, there are lots of instances of the flood fill function all in the memory, taking lots of space. For a large shape, this memory could be in the megabytes range. Not something to be ignored if you are working on a machine with limited memory (such as a mobile phone).
The moral is to be careful when dealing with recursion. There is a secret overhead because of the function calls, which should never be ignored.
Coming up next is a major example, and one of the best uses of recursion. It is harder than the previous examples, but it’s such a good example that I feel obligated to include it. Grab a cup of coffee; it should help. I’d treat you if I could…but let’s not digress. Instead, let’s go directly to our final example.
Connect 4 AI program
Our last example is creating an AI routine for a connect 4 program. Let’s assume you are creating a connect 4 game, and you want to write the routine for the computer player. How do you make the computer think? This is our example.
By the way, the same idea can be used to make an AI routine for nearly any turn based game, be it Tic-Tac-Toe, Connect 4, or even Chess. We will see in a moment what changes need to be made, and why the harder games require more work.
First, for those people who don’t know what Connect 4 is, it involves dropping pieces into a board to form a line of of 4 in a row, column or a diagonal. You can find an applet that plays Connect 4 at http://www.bodo.com/Applets/Connect4. I hope this gives you an idea of the game.
What is a good way to make the computer play this game? Here’s an idea. Computers are good at enumerating choices, so let’s make them do just that. Here is how we will make it work.
First, let’s consider this solution. The computer will try to put one of its pieces in each different location. Whatever makes the computer win, it will take it. What do you think of this solution? It is not very good. Why? Because the computer is not planning anything ahead. If the computer is only looking one move ahead, it is just like a man fighting in the dark, a man who can only see his footsteps. He will never be able to beat his opponent. Especially if his opponent can see and plan his moves very carefully.
Here is a better alternative. The computer will try to play a piece in each possible location. For each location, it will also try to put a piece for the opponent in each possible location, and then evaluate the position. It will choose the best position. Now, this is a bit better. The computer is now looking one move ahead.
But why stop here? We can extend this idea like this. The computer will try to place a piece in each possible location. For each location, it will try to put a piece for the opponent, and for each location it will try to place a piece for itself, and so on.
In other words the computer is trying all possible combinations for several moves ahead, and then it chooses the best move.
How should the computer decide the best move? It should assume that both sides are playing their best moves. So, when it tries its own moves, it should take the best move for itself, and when it considers the opponent’s move, it should take the worst move for itself. In other words, the best move for the opponent.
Getting complicated? Don’t worry, let me explain it in more detail. When I am trying my moves, if I find a really good move, then I certainly will take it. When I am enumerating my opponent’s moves, what if I find a really good move for him? He will certainly take it. It is that simple. When you are considering moves, you should take the best move for the side you are trying moves for.
The Minimax Method
What we have just created is called the minimax method. Let’s see roughly how our code should look. I will try to keep things as simple as possible, so as not to distract you from the main idea. I will explain how to improve on it later.
class MoveValue
{//holds a score, column pair.
int Score;
int col;
MoveValue(int s,int c)
{
Score=s;
col=c;
}
MoveValue()
{
Score=col=0;
}
}class Connect4
{
byte board[][]=new byte[8][7];
//the connect 4 board. 0 means empty, 1 means player 1 piece
//2 means player 2 pieceMoveValue bestMove(byte sideToMove,int depth,int maxDepth)
{//Finsd the best move for the side. Returns column and score pair
//depth specifies current level of depth (should be passed as 0)
//maxDepth specifies the maximum depth to search//holds the best score
MoveValue best_score = new MoveValue(-100000,0);
if(depth%2==1)best_score.Score*=-1;MoveValue mv=new MoveValue();
for (int i=0;i<7;i++)
{ //try putting a piece at each of 7 columns.
mv.col=i;
if(board[0][i]!=0)
continue; //if column is full ignore it.//find the row to put this piece. x is the row
int x=0;
while(x<8&&board[x][i]==0)x++;
board[x-1][i]=sideToMove;//find if it is a game over, don’t search further if it is
//a score of 100 or -100 means a game over
MoveValue cur=new MoveValue();
cur.Score=value();if(cur.Score==100||cur.Score==-100||depth==maxDepth)
mv.Score=cur.Score;
else //search further (this one is the recursive call)
mv.Score=bestMove((byte)(3-sideToMove),depth+1,maxDepth).Score;
//3-sideToMove, gives you the number of the opponent. If sideToMove is 1
//3-sideToMove will be 2. If sideToMove is 2, 3-sideToMove is 1//if this move is better, use it instead
if(mv.Score>best_score.Score&&depth%2==0)
best_score=mv;
else if(mv.Score<best_score.Score&&depth%2==1)
best_score=mv;//undo the move. Remove the piece you just put
x=0;
while(board[x][i]==0)x++;
board[x][i]=0;
}
return best_score;
}
}
This is the most complex piece of code we have written today. What does it do? Let me explain.
The code starts be defining a new class MoveValue, just to hold the value of a move, plus the column to play at. For example, if we discover that the best move is at column 3, which will give you a benefit of 90, the pair should hold the value (90,3).
Now, let’s turn our attention to the code itself.
The basic idea is simple. Each move is assigned a score based on how good this move is for the player. We choose the best move for the player by choosing the move with the highest score.
The code first declares a MoveValue variable called best_score to hold the best move it can find.
If depth is odd it initializes the score to a very small negative number, or else it initializes it to a big positive number. Huh? Why? Remember when we said the computer, when it tries its moves, should take the best move for itself, and when it considers the opponent’s move, it should take the worst move for itself. In other words, the best move for its opponent.
This is the reason behind those initial values. When depth is even, we are considering a move for the computer, so we initialize the score to a very small value so that the first possible move will be better than it. And when depth is odd, we initialize it to a very big number, so that any possible move will be worse than it.
Placing Pieces
Now what? We make a loop over all 7 columns and try to put a piece in each place. We try to put a piece in column #i if it has an empty slot.
After trying to put a piece we call a function called value() to tell us about the value of this position. The function value(), which we are going to cover in a moment, is given the current situation, and gives us a score that tells us how well this position is for the computer–higher is better, of course.
We call this function first to check for a game over. If it returns +100 the computer has won, or if it returns -100 the human player has won (this is how we are going to program it). In both cases, we don’t need to continue trying further moves. The game is over.
So, we check if the return from this function was +100 or -100 or if we have reached the end of our analysis (depth is equal to maxDepth) and we just take the value returned from the value() function.
If not, we recursively call the bestMove() function to continue digging deeper into the tree of possibilities.
In both cases, we check the return from the function against the best score found so far. If the new score is better than the best score found so far (i.e. it is larger than the best score if it is the computer’s turn, or smaller than the best score if it is the human’s turn), we replace the best score with the new score.
Finally, we return the board to what it was by removing the piece we just put. We repeat again for the next column and so on…
The Value Function
Now, what about that value function? This function should tell us how good a position is from the point of view of the computer player. For the sake of simplicity, we will make a simple value function that just checks who is winning. Here it is…
int value()
{//get the value of a position//check for 4 in a row
for(int i=0;i<5;i++)
for(int j=0;j<7;j++)
if(board[i][j]!=0&&board[i][j]==board[i+1][j]&&
board[i][j]==board[i+2][j]&&board[i][j]==board[i+3][j])
if(board[i][j]==1)
return 100;
else return -100;//check for 4 in a column
for(int i=0;i<8;i++)
for(int j=0;j<4;j++)
if(board[i][j]!=0&&board[i][j]==board[i][j+1]&&
board[i][j]==board[i][j+2]&&board[i][j]==board[i][j+3])
if(board[i][j]==1)
return 100;
else return -100;
//check for 4 in a diagonal
for(int i=0;i<5;i++)
for(int j=0;j<4;j++)
if(board[i][j]!=0&&board[i][j]==board[i+1][j+1]&&
board[i][j]==board[i+2][j+2]&&board[i][j]==board[i+3][j+3])
if(board[i][j]==1)
return 100;
else return -100;//check for 4 in the reverse diagonal
for(int i=3;i<8;i++)
for(int j=0;j<4;j++)
if(board[i][j]!=0&&board[i][j]==board[i-1][j+1]&&
board[i][j]==board[i-2][j+2]&&board[i][j]==board[i-3][j+3])
if(board[i][j]==1)
return 100;
else return -100;
return 0;
}
As you see, the function just checks if there are 4 pieces in a row, column or diagonal, and if it is the computer’s pieces, it returns +100, and if it’s the human’s pieces it returns -100.
Trying it Out
Whew, and that’s it. Now we have a nice Connect 4 AI engine that can actually look several moves ahead, thanks to recursion. For example, if we try the engine on this position:

Fig 4. The AI (red player), will know how to win in this situation
If the program is called to play for the red player with a depth of only 3, it will correctly find that the best move is to put a piece in column 2 to force the human to block at column 5 and then the computer wins by putting a piece at column 5.
In another situation:

Fig 5. If the computer (yellow player) could only see one step ahead, he would be unable to see the threat of playing at column 3 or 6 and winning. Now, it can detect and prevent it.
The computer correctly decides to play at column 3 to block the human from winning by putting at column 3 or 6.
Pretty impressive, huh? And it is all less than 30 lines of code.
Two More Tips
(Page 10 of 10 )
So, is this the end of story? Certainly not! There is room for improvement, but that’s really beyond the scope of this article. I will just give you 2 pointers. First, the value() function could be greatly enhanced, so that it gives a better scoring system. For example it should give some score for 3 in a row (because that’s close to getting 4 in a row). If further studies are made about the game, we can even create a much better value function that takes into account the position of pieces relative to each other, and to the edges and so on…
On the other hand, we can also improve on the bestMove() function itself, by using the alpha beta pruning technique. This technique makes minimax run a lot faster, and I mean a lot. Sadly, this is also outside the scope of this article, but you will find references to alpha beta pruning at the end. Maybe this should be the topic of a future article. Drop me an email if you are interested.
One last comment. The routine we made ignores the case when the board gets full and the game ends in a draw. I didn’t want to complicate things, but I am sure you can easily add it to the current code without much trouble.
So, that was it. Our last example for recursion. But this not the end of the journey. The journey has just started. Hopefully you are ready to start writing your own recursive code now.
When to Use (or Avoid) Recursion
As we saw with the flood fill program, sometimes recursion wastes a lot of memory space (or execution time). Other times, recursion is just the way to go. How do you decide?
Finally a bit of theory, for theory fans. Every recursive program can be re-written in a non-recursive way. Period. But this simple statement doesn’t indicate how complicated the non-recursive code can be. Sometimes a 3 liner recursive code can become truly monstrous code when you switch into the non-recursive version. It is always your decision.
—
by Mohamed Saad
The PHP 5 release comes with a slew of new features aimed at simplifying development with PHP. With PHP 5 comes the introduction of exception handling, the Standard PHP Library (SPL), enhanced support for XML, reflection, and quite a few enhancements to the object oriented features of the language. PHP 5 also offers a sizable list of new functions, many of which will not be covered in this article but are available in the manual.XML
PHP 5 by default installs XML support and offers a new extension, SimpleXML. All XML functions are now standardized on the libxml2 library and are fully W3C standards compliant. SimpleXML is quite possibly the most valuable addition to PHP in years, providing a traversable structure to work on XML documents, allowing you to simply change values in the structure and write the file out with only a few lines of code. The XML functionality that has been available through PHP in the past has been quite rudimentary and required a fair amount of programming work to use, so it is not uncommon to see PHP 4 applications using XML without ever touching the xml functions.
PHP 5 also offers a replacement extension for DOMXL (available in “experimental” form in PHP 4) with the DOM extension. This extension allows you to work on your XML files using the DOM object model and is superior to SimpleXML particularly when you are not certain what document format to expect with your application. While DOM is more powerful, SimpleXML is much quicker to implement and easier to get a handle on for beginner programmers. Both of the extensions are robust and well thought out, and whichever suits your programming needs and taste, you will be using a powerful extension that is light years beyond what was available in PHP 4.
Database Support
PHP 5 offers some big enhancements to its ability to interact with databases. The most significant addition is the embedded SQLite database, a quick, lightweight database engine made specifically for embedded applications. This means there is no RDBMS process running on the server; SQLite reads and writes directly to files on disk. This results in significantly lower memory overhead when the database is not being used, but major performance problems arise if the system is used in a high traffic environment. SQLite is intended for small scale use, as best I can gather.
When testing it with small tables and less than one thousand rows per table, it was comparable to MySQL in executing simple joins with only one concurrent request, but performance from SQLite degraded exponentially with five or more concurrent connections coming in, which makes perfect sense. This is a good database solution for a small site that needs minimal features and expects minimal usage. It could also be useful for storing embedded configuration data in a PHP 5 application that may house its main data store in another RDBMS, and only run small queries against SQLite. SQLite is relatively standards compliant with a few major exceptions, most notably the lack of an ALTER TABLE statement.
PHP 5 also introduces support for the MySQL 4.1 client library with the introduction of the mysqli extension. The mysqli extension provides some basic objects for working with the MySQL server. The mysqli class is used as a connection object and as the ability to open and close connections as well as get context and state information from the server. The mysqli_stmt class represents a prepared statement that allows you to execute “prepare” and “execute” queries against the database. Lastly, the mysqli_result object provides a cursor based interface for reading results, providing similar functionality to the functions available in the standard MySQL extension using a MySQL resource handle. The new extension also adds support for SSL and input/output parameters.
The last notable addition in the database area is enhanced support for Firebird/InterBase, an RDBMS that offers most ANSI SQL-92 features and runs on most operating systems. The ibase extension provides most of the same functionality for Firebird/InterBase as the new mysqli extension does for MySQL but in the same manner as the old MySQL extension - that is, no objects.
SPL: Exceptions and Iterators
PHP 5 comes with the Standard PHP Library (SPL), a collection of objects built to handle various tasks such as exception handling and object traversal (iteration). There are basically six groups of classes/interfaces available natively to the SPL.
Each iterator has a specific purpose and details can be found in the PHP manual.
OOP: Object Enhancements
PHP 5 makes leaps and bounds in its support for objects. Aside from all the new features, Zend claims to have addressed the performance problems involved with object creation and usage in previous versions of PHP, a fact that in itself should encourage more developers to use object-based PHP. PHP 5 offers enhancements in a few key areas including object autoloading, destructors, visibility, static methods, class constants, type hinting, interfaces, cloning, reflection and several magic methods.
Autoloading is a great new feature that provides a way for developers to make sure all dependencies for a class are in place before using it. If you attempt to instantiate a class that has not yet been defined, PHP 5 will call the __autoload() function as a last attempt to load the class before failing with an error. Since most developers put one class per file, and many classes often depend on another either by way of inheritance or encapsulation, __autoload() allows you to make sure all the necessary class files have been included. While PHP 4 had constructors, PHP 5 offers a new features: destructors. Destructors are called when an object is destroyed or all references to it have been removed. Destructors are implemented in classes by use of the __destruct function. The __construct function has also been introduced and takes precedence over the old-style constructor function. The old style still works, but it is recommended that __construct is used as it takes higher priority.
One of the biggest additions to PHP’s support for objects is visibility modifiers, also known as access modifiers. The var keyword has been deprecated and class variables are now to be declared as public, private, or protected. Public class variables are available to any other part of the program. Private variables are only available to that class. Protected variables are available to a class as well as its child classes, unlike private which only allows the class itself to access the variable. Methods can also be declared as public, private, or protected, and if none are declared a method is assumed to be public. Access modifiers allow PHP programmers to programmatically hide the inner workings of one object from another by preventing other objects from accessing class data directly. PHP 5 also introduces the static keyword. Static methods are called without an object instance and calls to static methods are resolved at compile time, not runtime. Static properties are accessed by the :: operator, not ->, and the special variable $this is not available in static methods.
Not only can you declares constants in C-style syntax, const constant = ‘constant value’;, but a class can contain it’s own constants and access them internally using self::constant. This simplifies management of constants and keeps them contained within the classes they belong to, preventing code clutter and conflicts with other constants in the same application that may need the same name but a different value. PHP 5 also allows type hints in method parameter declarations. If a parameter is given a type hint and an object of the wrong type is passed to it, PHP will generate a fatal error. It would be preferable for an exception to be thrown, but type hinting does at least allow responsibility to be placed in the calling code for making sure the proper data type is passed into a function call. Type hints can be used in any function, not strictly in class methods.
PHP 5 also introduces abstract classes and interfaces, which is probably THE most significant enhancement to the language. Abstract classes and interfaces allow high level design principles to be semantically applied to PHP classes. PHP includes three special method keywords, final, abstract and virtual, to facilitate the use of inheritance and interfaces. When a method is declared as final, it cannot be overridden by a child class. When a method is declared as abstract, it must be defined in a child class. When a method is declared as virtual, it may be inherited as is or overridden by a child class. When used in combination, abstract classes and interfaces enforce high level design throughout all levels of implementation and support properly coded objects. They can be used in excess, as can anything else, but used properly these are the most powerful tools available to object oriented developers.
The last features of note are object cloning, some more magic functions and the reflection class. Object cloning allows the implementation of a magic method, __clone() to define what exactly takes place when clone is called on an object. It allows developers to implement deep copying of object data when cloning without writing messy code. A few other noteworthy magic functions are __sleep() and __wakeup(), which are used in conjunction with serialize and unserialize to ensure proper destruction and recreation of resources used within an object. Additionally, the __toString magic method allows a class to decide how to react when it is used in string context. The reflection class works as the name implies; it allows developers to programmatically reverse-engineer classes, interfaces, functions and extensions. Reflection is a powerful tool in developing custom application frameworks.
Conclusion
Overall, PHP 5 offers dramatic improvements over PHP 4 in a lot of areas. These improvements are, by and large, geared toward advanced PHP developers. With the exception of the SimpleXML extension, most of the new functionality will probably have no appeal to the largest segment of the PHP programming population - that is, developers who look at PHP as a simple scripting language and use it to accomplish one task at a time. For PHP application developers though, I see PHP 5 quickly becoming the de facto standard. Start putting pressure on your hosts now to upgrade, because PHP 5 was definitely worth the wait.
—
by David Fells
22 Sep
Posted by ProCOM
on September 22, 2007 – 9:11 pm - 396 views
While most of the excitement surrounding the release of PHP 5 focused on its XML and object-oriented features, the Standard PHP Library (SPL) also saw some significant improvements that went mostly unnoticed. In the first of two articles covering the SPL, David Fells discusses the Exception class, which lets programs handle errors more gracefully and simplifies debugging.Introduction
Amid all the noise and excitement surrounding the release of PHP and its shiny new XML and object oriented features, the Standard PHP Library (SPL) slipped by without much recognition. Exception handling has been a major feature add for PHP 5, and the classes built into PHP are a part of the SPL. This article will demonstrate basic application of several SPL classes and provide information on the classes and interfaces available to PHP 5 programmers.
This article is the first part in a series discussing the classes available in the SPL and their uses. In this article we will discuss the Exception class and its application. Exceptions serve a dual purpose - they allow applications to gracefully handle errors, and they allow simplification of the debugging process, which adds up to a more efficient development process and less stress for developers.
Exceptions
Exceptions have been around for quite a while in languages like C++ and Java, but simple scripting languages have never had support for this graceful error handling device. That changed with the release of PHP 5. Exceptions are not all that special as far as objects go; they are essentially containers for error information. The interesting part is how exceptions are created and identifed. To use exceptions, one only needs to be familiar with the special control structures and functions associated with them. They are built-in and require no configuration and only a handful of brain cells. Exception handling requires proper use of “try”/”catch” blocks and the use of the “throw()” function.
Exceptions are created, or thrown, in one of two ways. Either a developer tells the application to throw an exception when a specific error condition is met (such as data out of the expected range as a function parameter) or the language itself throws an exception when it encounters an application error. The latter type are generated from a lower level in terms of system architecture, but the end result is the same: developers can catch exceptions that are thrown by application code and either ignore them, attempt to recover or gracefully fail. Any option is better than creating a web of error handling methods (such as the PEAR Error class) or by using “die()”, but exceptions are, pun intended, an exceptional solution to the problem of graceful, clean error handling.
Simple Example
Now that we have covered the basics of what an exception is, we will look at a code snippet to see them in action.
class Math {
function divide($divisor, $dividend = 1) {
if (!is_numeric($divisor))
throw(new Exception(’Divisor is not a number’));
if (!is_numeric($dividend))
throw(new Exception(’Dividend is not a number’));
if ($dividend == 0)
throw(new Exception(’Division by zero’));return ($divisor / $dividend);
}
}try {
print Math::divide(1, 0);
}
catch(Exception $e) {
print $e;
}
If you run this code sample, you will see a dump of the contents of the exception thrown by our divide function. The object contains a lot of useful info like the file name and line where the error occurred and the trace. If you are logging errors or displaying them to the screen (for debugging), this information is priceless. Let’s go over the important parts of this code.
if (!is_numeric($divisor))
throw(new Exception(’Divisor is not a number’));
if (!is_numeric($dividend))
throw(new Exception(’Dividend is not a number’));
if ($dividend == 0)
throw(new Exception(’Division by zero’));
This piece of code checks to see if the divisor and dividend are numbers and if the dividend is a non-zero value. If any of these checks fail, our method calls the new function “throw()”, passing it a new Exception object. We send our message to the constructor of Exception so the debugging information is descriptive enough to be of use and to indicate the specific type of failure encountered by the function. Exceptions can also be extended to give them meaningful names, such as DivisionByZeroException for our case, but in general supplying a suitably descriptive message is adequate.
try {
print Math::divide(1, 0);
}
Here we attempt to call “divide()”. Placing it in a “try” control structure allows us to logically separate our code blocks, as not all operations require exception handling. We could generate other errors by making calls to divide with non-numeric input.
catch(Exception $e) {
print $e;
}
This is where the exception is actually caught for handling. We could do something with this error, such as tell the user they cannot divide by zero with the “divide()” function, but in this case we are simply invoking the “$e->__toString()” magic method by treating “$e” as a string - ultimately yeilding an informative error message for the developer.
The Exception Class Hierarchy
As I noted before, the Exception object is essentially a data container from the developer’s standpoint. The only overridable method is “__toString()”, a magic function invoked by default when the class is used in string context. Below is the class definition for the built-in exception class.
class Exception
{
protected $message = ‘Unknown exception’; // exception message
protected $code = 0; // user defined exception code
protected $file; // source filename of exception
protected $line; // source line of exceptionfunction __construct($message = null, $code = 0);
final function getMessage(); // message of exception
final function getCode(); // code of exception
final function getFile(); // source filename
final function getTrace(); // an array of the backtrace()
final function getTraceAsString(); // formated string of trace/* Overrideable */
function __toString(); // formated string for display
}
The methods provided by the Exception class allow a developer to get all relevant information required to debug an error. The most useful feature is the stack trace, which was previously only available to developers using the Zend Debugger. The stack trace allows you to follow the code path that generated the exception from end to end, tracing back through calling methods, files and lines, until the source function call is displayed.
In complex applications, this information is truly priceless and eliminates, to some degree, the ad hoc debugging methods typically employed in PHP to trace errors such as inserting “die()” or “print_r()” calls at key places in your code to get a trace. You will still have to do these things from time to time but overall, Exceptions should ease debugging pains considerably. Other methods allow you to retreive the message and code that were passed into the constructor, the stack trace in string format (instead of an array), and the file name and line number where the exception was thrown.
Like most other clases, the base class Exception can and should be extended to fit its intended usage. The purpose of extending the Exception class is, almost exclusively, to allow instantiation of Exceptions with intuitive, specific names rather than the nonspecific (but still intuitive) Exception.
Developers should be very careful in how they extend the Exception class, as the purpose of the class itself requires minimal functionality.If a task needs complex work to take place before an Exception can be built with the proper data, then you would be right to put that functionality into the Exception, but adding functionality for functionality’s sake is not the right approach. For example, you would not create several methods to output an error message in various formats. There are already quite a few children of the Exception class in the SPL, all of which are made for use in other SPL classes or by the runtime itself.
A Simple Exception Extension

Using our example code above, we can demonstrate a simple extension of the Exception class.
class DivisionByZeroException extends Exception{}
class WrongParameterTypeException extends Exception{}
class Math {
function divide($divisor, $dividend = 1) {
if (!is_numeric($divisor))
throw(new WrongParameterTypeException(’Divisor is not a number’));
if (!is_numeric($dividend))
throw(new WrongParameterTypeException(’Dividend is not a number’));
if ($dividend == 0)
throw(new DivisionByZeroException(’Division by zero’));return ($divisor / $dividend);
}
}try {
print divide(1, 0);
}
catch(WrongParameterTypeException $e) {
print $e;
}
catch(DivisionByZeroException $e) {
print $e;
}
Using this approach, we are able to easily handle different errors in different ways, and this is where the real value of extending the Exception class comes in. We can chain an unlimited number of catch blocks together to capture the different types of exceptions that could have occurred. We could accomplish a similar feat using error codes (remember the second parameter to the Exception class constructor). To do this we would typically define class constants to identify the error type. We could ammend our code to look like this, for example.
Class Math {
const DIVISION_BY_ZERO = 1;
const WRONG_PARAMETER_TYPE = 2;public static function divide($divisor, $dividend = 1) {
if (!is_numeric($divisor))
throw(new Exception(’Divisor is not a number’, self::WRONG_PARAMETER_TYPE));
if (!is_numeric($dividend))
throw(new Exception(’Dividend is not a number’, self::WRONG_PARAMETER_TYPE));
if ($dividend == 0)
throw(new Exception(’Division by zero’, self::DIVISION_BY_ZERO));return ($divisor / $dividend);
}
}try {
print Math::divide(1, 0);
}
catch(Exception $e) {
if ($e->getCode() == Math::WRONG_PARAMETER_TYPE))
die(’You passed divide() a non-numeric value’);
if ($e->getCode() == Math::DIVISION_BY_ZERO))
die(’You tried to divide by zero’);
print $e;
}
The same end result is achieved with a different approach. Ultimately this is a matter of taste, but in my experience I have found it easier to keep up with exception classes. Regardless of your individual taste in approaches, this method of error handling is far more elegant and intuitive than anything available in PHP up until now, so take advantage of it.
Conclusion
Exceptions serve two main purposes: to simplify debugging and to allow for graceful error handling. They alllow a clean structure for identifying and creating errors at the script level, and allow simplified error recovery. The PHP 5 implementation of exceptions is, for all intents and purposes, identical to exception handling in Java, .NET and other languages capable of exception handling. Proper use of exceptions in PHP applications should help shed blobs of ugly legacy error handling and clean up code in general considerably. It allows the elimination of a lot of bulky error checks and the consolidation of error handling and recovery routines. Exceptions should be practiced religiously in PHP 5 applications.
Our next installment will discuss another important chunk of the SPL: Iterators. See you soon!
—
by David Fells
This article explains what polymorphism is and how it applies to object oriented design in particular. It also explains the pros and cons of polymorphism when working with certain versions of PHP.NOTE: The latest versions of PHP, and I am not sure in which exact version this was corrected, support late binding properly. There are still tons of issues with using late binding in cahoots with a bytecode cache, for reasons beyond my understanding or concern. Those of you using an older version of PHP (I still have 5.0.1 on a server) can see for yourselves the lack of late binding support. I initially rescinded this article after learning that late binding is properly supported in current versions, but it is important that you be aware of the possibility that it may not work in your particular PHP 5 install.
PHP 5 and Polymorphism
This article is going to cover one of the most important parts of object oriented programming and design–polymorphism–using PHP 5 for demonstration purposes. Before you continue, be warned that this article is not entirely positive in regard to PHP. While the language is great for rapid development and has made tremendous strides in the last two major versions, its object support still has a way to go before being on par with more mature languages like C++ or Java.
If you are an object programming guru this article probably isn’t for you, since polymorphism is one of those things you learn light-switch style – suddenly it’s all clear, and once you understand it, you never forget. If you are looking to learn a bit about object programming and design and you aren’t really sure exactly what it means when someone says an object is polymorphic, this article will help.
By the end of this article, you should know what polymorphism is and how it applies to object oriented design in particular, and you should know about the pros and cons of PHP 5 object programming as it pertains to polymorphism.
What is Polymorphism?
Polymorphism – “The occurrence of different forms, stages, or types in individual organisms or in organisms of the same species, independent of sexual variations.” (dictionary.com). By that definition, we could assume polymorphism is a programmatic way to represent the same object through multiple states or stages. That’s great, but probably still unclear to many of us. What it really means is this: programming to an interface or base class without regard to an object’s concrete class.
If you are familiar with design patterns, even on a rudimentary level, you should be getting a picture in your mind of what this means. In fact, polymorphism is probably the single greatest facilitator for patterns-based programming. It allows us to organize similar objects in a logical way such that calling code does not have to worry about specific types; rather, we code to an expected interface or base class. The more a software application is abstracted, the more flexible it becomes – and polymorphism is one of the best ways to abstract behaviors.
For example, consider a class called Person. We can subclass Person with classes called David, Charles, and Alejandro. Person has an abstract method called AcceptFeedback(), which all subclasses implement. That means that any code using any subclasses of the Person base class can call the AcceptFeedback() method with confidence, knowing that the class itself handles logic that would otherwise appear in a conditional. You do not have to check whether the object is a David or an Alejandro, just that it is a Person. The effect is that your code is written to the lowest common denominator – the Person class.
The Person class in this example could also be created as an interface. There are some differences, primarily that an interface imparts no behavior, only a set of rules, so to speak. A Person interface would say “you must support the AddFeedback() method” whereas a Person class could provide some default code for the AddFeedback() method, saying “if you choose not to support AddFeedback(), you will be provided with a default implementation.” Choosing interfaces or base classes is the subject for another article entirely, but in general, if you need a default implementation for a method, provide it through a base class. If you are simply outlining a set of expectations for your classes, then use an interface.
Applying Polymorphic Design
Continuing with our Person base class example, let’s take a look at a non-polymorphic implementation. The following example shows a really poor way to create an application that uses different types of Person objects. Note that the actual Person classes are omitted; we’re only concerned with the calling code for now.
<?php
$name = $_SESSION[’name’];
$myPerson = Person::GetPerson($name);
switch (get_class($myPerson))
{
case ‘David’ :
$myPerson->AddFeedback(’Great Article!’,
‘Some Reader’, date(’Y-m-d’));
break;
case ‘Charles’ :
$myPerson->feedback[] = array(’Some
Reader’, ‘Great Editing!’);
break;
case ‘Alejandro’ :
$myPerson->Feedback->Append(’Awesome
Javascript!’);
break;
default :
$myPerson->AddFeedback(’Yay!’);
}
?>
This example shows objects that behave differently and a switch statement to differentiate between the different classes of Person, performing the correct operation on each. Note that the feedback comment in each condition is different. That probably would not be the case in a real application; it’s simply done to elucidate the differences in the class implementations.
The next example uses polymorphism.
<?php
$name = $_SESSION[’name’];
$myPerson = Person::GetPerson($name);
$myPerson->AddFeedback(’Great Article!’, ‘SomeReader’, date(’Y-m-
d’));
?>
Note the lack of the switch statement and, of greater importance, the lack of concern regarding what type of object Person::GetPerson() returned. Person::AddFeedback() is a polymorphic method. The behavior is one hundred percent encapsulated by the concrete class. Remember, whether we’re working with David, Charles or Alejandro, calling code never has to know the concrete class to function, only the base class.
While I’m sure there are better examples than mine, I think it demonstrates the basic use of polymorphism from the perspective of calling code. We now need to take into consideration the internals of the classes. One of the greatest aspects of inheriting from a base class is that the inheriting class is able to access the behaviors of the parent class, which often serve as nothing more than defaults, but can also be chained to inheriting methods to create more sophisticated behaviors. Below is a simple demonstration of this.
<?php
class Person
{
function AddFeedback($comment, $sender, $date)
{
// Add feedback to database
}
}
class David extends Person
{
function AddFeedback($comment, $sender)
{
parent::AddFeedback($comment, $sender,
date(’Y-m-d’));
}
}
?>
Here we have chained the David::AddFeedback() method to the Person::AddFeedback method. You may note that it resembles overloaded methods in C++, Java, or C#. Remember that this is a simplified example, and that the actual code you write will of course be completely dependent on your project.
Late Binding in PHP 5, or, the Lack Thereof
(Page 4 of 4 )
In my opinion, late binding is what makes Java and C# so spectacular. They allow base class methods to invoke methods in “this”, or $this, even if they do not exist in the base class–or, better yet, to invoke a method from the base class that may be replaced by another version in an inherited class. You may think something like this would work in PHP:
<?php
class Person
{
function AddFeedback($messageArray)
{
$this->ParseFeedback($messageArray);
// Write to database
}
}
class David extends Person
{
function ParseFeedback($messageArray)
{
// Do some parsing
}
}
?>
Keep in mind that there is no ParseFeedback in the Person class. Now, say you have this implementation code, which, for the sake of example, results in $myPerson being a David object:
<?php
$myPerson = Person::GetPerson($name);
$myPerson->AddFeedback($messageArray);
?>
PARSE ERROR! The method ParseFeedback does not exist, or something along those lines. Sigh! So much for late binding in PHP 5! Wait–you probably want to know what late binding is.
Late binding means that method invocations bind to the target object at the last possible minute, which means those objects already have a concrete type when the method is invoked by the runtime. In our example above, you would be calling David::AddFeedback(), and since $this in David::AddFeedback() references a David object, you would logically assume that the ParseFeedback() method exists–but it does not, because AddFeedback() was defined in Person, and calls ParseFeedback() from the Person class.
Unfortunately there is no simple way to skate around this rotten behavior in PHP 5, which means you’re somewhat neutered when it comes to creating a flexible polymorphic class heirarchy.
I must point out that I chose PHP 5 as the language for this article simply for this reason: it’s not very good for abstracting object concepts! Consider it a cautionary tale from someone who learned it the hard way, beginning with PHP 5 when it was still in alpha and assuming that since they added abstract classes and interfaces, late binding was a given.
The End
You should now have a basic understanding of polymorphism and why PHP 5 is not very good at implementing it. You should, generally speaking, know how to encapsulate conditional behavior with a polymorphic object model. Ultimately what you should gain from this is flexibility in your objects, which of course means less code overall. You may end up creating a few more class and a few more class methods, but your calling code will be greatly reduced, which almost always results in time and effort savings. You also increase the clarity of your code by encapsulating conditional behavior-–behavior based on an object’s state-–within the object itself, rather than handling it with code that, for all intents and purposes, should not know enough about the object to make any real decisions on such matters.
If, like myself, you find yourself to be passionately interested in object oriented design, read what I consider to be the three fundamentals: “Design Patterns” (ISBN: 0201633612), “Refactoring” (ISBN: 0201485672), and “Refactoring to Patterns” (ISBN: 0321213351). I would suggest reading them in the stated order. They provide a far more extensive and expert opinion than my own, and will set you on the path to good object oriented software design.
—
by David Fells
PHP has the drawback of not supporting events. Fortunately, a basic structure can be built to support events in PHP 5. This article tackles that problem with some proof of concept code.One of the big drawbacks of PHP has always been its lack of events. Events allow programmers to attach new behaviors to objects that are activated when certain conditions are met. Those conditions are announced to the outside world as