Chapter 15. Application Techniques

By now, you should have a solid understanding of the details of the PHP language and its use in a variety of common situations. Now we’re going to show you some techniques you may find useful in your PHP applications, such as code libraries, templating systems, efficient output handling, error handling, and performance tuning.

Code Libraries

As you’ve seen, PHP ships with numerous extension libraries that combine useful functionality into distinct packages that you can access from your scripts. We covered using the GD, FPDF, and Libxslt extension libraries in Chapters 10, 11, and 12, respectively.

In addition to using the extensions that ship with PHP, you can create libraries of your own code that you can use in more than one part of your website. The general technique is to store a collection of related functions in a PHP file. Then, when you need to use that functionality in a page, you can use require_once() to insert the contents of the file into your current script.

Note

Note that there are three other inclusion type functions that can also be employed. They are require(), include_once(), and include(). Chapter 2 discusses these functions in detail.

For example, say you have a collection of functions that help create HTML form elements in valid HTML: one function in your collection creates a text field or a text​area (depending on how many characters you set as the maximum), another creates a series of pop ups from which to set a date ...

Get Programming PHP, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.