13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

158 Chapter 5 Reusing Code <strong>and</strong> Writing Functions<br />

There is something very elegant <strong>and</strong> mathematical about recursive solutions. In most<br />

cases, however, you are better off using an iterative solution.The code for such a solution<br />

is also shown in Listing 5.5. Note that it is no longer (although this is not always the case<br />

with iterative functions) <strong>and</strong> does exactly the same thing.The main difference is that the<br />

recursive function makes copies of itself in memory <strong>and</strong> incurs the overhead of multiple<br />

function calls.<br />

You might choose to use a recursive solution when the code is much shorter <strong>and</strong><br />

more elegant than the iterative version, but it does not happen often in this application<br />

domain.<br />

Although recursion appears more elegant, programmers often forget to supply a termination<br />

condition for the recursion.This means that the function will recur until the<br />

server runs out of memory, or until the maximum execution time is exceeded, whichever<br />

comes first.<br />

Namespaces<br />

In general, a namespace is an abstract container that holds a group of identifiers; in <strong>PHP</strong><br />

this means that namespaces can contain the functions, constants, <strong>and</strong> classes that you<br />

define. There are several organizational advantages for creating namespaces for your custom<br />

function <strong>and</strong> class definitions, including:<br />

n All functions, classes, <strong>and</strong> constants within a namespace are automatically prefixed<br />

with the namespace name.<br />

n Unqualified class, function, <strong>and</strong> constant names are resolved at runtime, with the<br />

first search taking place in the namespace before moving out to the global space.<br />

n All functions, classes, <strong>and</strong> constants within a namespace are automatically prefixed<br />

with the namespace name.<br />

For more information <strong>and</strong> practical examples of namespaces in <strong>PHP</strong>, please see the <strong>PHP</strong><br />

Manual at http://www.php.net/language.namespaces<br />

Further Reading<br />

The use of include(), require(), function, <strong>and</strong> return are also explained in the<br />

online manual.To find out more details about concepts such as recursion, pass by value<br />

or reference, <strong>and</strong> scope that affect many languages, you can look at a general computer<br />

science textbook, such as Dietel’s <strong>and</strong> Dietel’s C++ How to Program.<br />

Next<br />

Now that you are using include files, require files, <strong>and</strong> functions to make your code<br />

more maintainable <strong>and</strong> reusable, the next chapter addresses object-oriented software <strong>and</strong><br />

the support offered in <strong>PHP</strong>. Using objects allows you to achieve goals similar to the concepts<br />

presented in this chapter, but with even greater advantages for complex projects.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!