|PHP 5 Power Programming|
|author||Andi Gutmans, Stig Saether Bakken, Derick Rethans|
|summary||PHP 5 Power Programming|
Chapter 1 sets the stage by asking "What Is New in PHP 5"? The chapter begins with a quote from John Scully: "The best way to be ready for the future is to invent it," which perfectly describes the authors: Andy Gutmans and Zeev Suraski have continued to push the PHP project to improve on the original foundation and to add cleaner programming enhancements. The leap from v.4 to v.5 is no exception, as this new version has brought singular advances: OO programming with PHP enjoys a leap forward in focus with a complete redesign of its object model, a completely re-written MySQL extension (MySQLi), support for SQLite, a new mechanism for handling errors via exceptions, a suite of re-written XML extensions based on the libxml2 library, a C-based implementation of SOAP and a new memory manager.
Following this tour of the new features of PHP 5, the reader is exposed to a brief chapter on the basic language of PHP. An experienced developer can skip this chapter, but it's a very good overview of the syntax including variables and superglobals, basic data types (integers, strings, booleans, arrays, constants), operators, control structures (conditional, loop, code) and functions.
PHP 5's OO support is the subject of Chapter 3. Object oriented programming was introduced in PHP 3 but it was in an extremely elementary form and while it was improved upon in PHP 4, it truly comes into its own in PHP 5. Covered in this chapter are the basics of the OO model, object creation and lifetime, access restriction keywords and the benefits of using class inheritance, as well as tips for exception handling.
Chapter 4 is a well written chapter on advanced OOP and design patterns (strategy, singleton, factory and observer), with some very good code examples on iterators and the reflection API, which allows the programmer to collect information about his or her script at runtime.
Chapter 5, "How to Write a Web Application with PHP," is an excellent fusion of code and best practices in developing a complete dynamic web application based on PHP. The concept of good design practices is often overlooked in "how-to" manuals. Rather, many technical books focus on overcoming code issues and avoiding pitfalls associated with poor code execution. Here, the authors take time out to detail techniques on making scripts "safe" ("trust nobody, especially not the users of your web applications"). Bugs and security holes are a fact of life, and this chapter covers some very practical techniques to avoid consequences arising from weirdness caused by users as well as from deliberate attempts by bad guys to crack a site's security.
Databases are covered in depth in Chapter 6. SQL and SQLite are discussed (each with a section on strengths and weaknesses) before examining the new database-connectivity features of PHP5 using mysqli and sqlite extensions. PEAR DB is also presented, with a section on the pros and cons of using a database abstraction layer like PEAR DB. Connections, queries, fetching modes/results and other topics are well covered. I would have liked to see some additional discussion and implementation of SMARTY in this chapter. SMARTY is a template engine with an ability to cache templates into PHP scripts which saves on overhead and contributes to speed and efficiency.
Error handling is the topic of Chapter 7. Types of errors (undefined symbol errors, portability errors, runtime errors and PHP errors), PEAR errors (PEAR_error class, handling of PEAR errors and PEAR error modes) and exceptions (exceptions explained and the specifics of using exceptions) are treated adroitly.
"XML with PHP 5" in Chapter 8 will undoubtedly become a focal point of the book for many readers. Along with the addition of support for OO programming, using the new XML extensions are some of the most exciting developments in PHP 5. The XML implementation is standardized on libxml2, compliant with W3 standards and extremely efficient. This chapter introduces XML in PHP 5 with sections on the vocabulary of XML, parsing (SAX, DOM, XPath), the SimpleXML extension (very cool, because this allows the programmer to access the XML through a data structure representation, treating the information as objects), PEAR classes that deal with XML (XML_Tree, XML_RSS), converting XML and communicating with XML (XML-RPC, SOAP). PHP 5's new SOAP extension is a welcome improvement over previous PHP versions.
Following this excellent chapter is one on other valuable mainstream extensions: files and streams (I/O streams, compression streams, URL streams, locking, renaming and removing files, temporary files), regular expressions, date handling, graphics manipulation with GD ("gif draw" to old-timers, "graphics draw" to the youngsters) and multi-byte strings and character sets. This is a solid chapter encompassing the wide range of functions that are intrinsic to the core of PHP to the many favorite and practical extensions that are outside the core of PHP.
Chapters 10 through 12 deal with PEAR: installing PEAR, commands, packages and components. Stig Bakken's extensive knowledge and experience is obviously prevalent here. It's worth noting that you won't find a better single coverage of PEAR and PHP 5 anywhere else.
For those readers experienced in PHP 4 and who are looking to move to v.5 and are wondering what to expect during the transition, Chapter 13, "Making the Move", will be of particular interest. The authors suggest that in migrating to PHP 5, "you can encounter some minor incompatibilities" and address a number of these: using compatibility mode to revert to PHP 4 behavior, recognizing script problems using OO features and learning the new names and locations of files in the PHP 5 distro, among others. Users of PHP on the Windows platform may want to spend some time over this chapter (and maybe reconsider their choice of development platform!).
Designing for performance is the subject of Chapter 14, and the authors encourage the reader to plan for optimal performance during the design phase: benchmarking, profiling with Zend Studio's Profiler, APD (Advanced PHP Debugger) and Xdebug, using APC (Advanced PHP Cache) and ZPS (Zend Performance Suite), optimizing code using micro-benchmarks, rewriting in C and writing procedural versus OO code. This extensive chapter offers the reader a fairly complete set of tools and sage advice for more efficient design.
Chapter 15 is titled "An Introduction to Writing PHP Extensions," and introduces the extension API that allows developers to write custom PHP extensions. This isn't a chapter for everyone, since there are already a large number of available extensions and, as the authors note, unless you need to wrap an existing C library to give it an interface from PHP then you can easily skim or skip this chapter entirely. Note, though, that memory management has a section here and it's worth a read because of PHP 5's support for multi-threaded environments.
The final chapter of the book is called "PHP Shell Scripting," and explores the CLI SAPI (command line interface Server API). There is an introduction to PHP CLI shell scripts (how CLI differs from CGI, the shell scripting environment, parsing CL options, good practice) with some nice examples.
Appendices on PEAR and PECL (PHP Extension Community Library) Package Index, phpDocumenter format Reference and Zend Studio Quick Start Guide complete the volume.
The authors succeed in providing an excellent manual for "power programming" in PHP 5. There is terrific guidance here for many PHP developers and experienced Java, C++ and C# coders who are looking to either migrate to PHP 5 from v.4 or who are looking to gain experience in PHP programming. The experience the authors bring to the table is indisputable and their style of writing and the ease with which they bring new language and tools to their audience is admirable.
You can purchase PHP 5 Power Programming from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.