Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror
×
Book Reviews Books Media

PHP In Action: Objects, Design, Agility 232

Michael J. Ross writes "Despite being perhaps the most popular Web language in use, PHP has for much of its history been criticized for not offering the full capabilities of object-oriented programming (OOP). But with the release of version 5, PHP introduced a robust object model, and made it easier for its proponents to create well-architected Web sites and applications. In turn, the new OOP capabilities have facilitated additional best practices, such as design patterns, test-driven development, continual refactoring, and HTML templates. These topics and more are explored in the book PHP in Action: Objects, Design, Agility."
PHP in Action: Objects, Design, Agility
author Dagfinn Reiersol, Marcus Baker, Chris Shiflett
pages 552
publisher Manning Publications
rating 8/10
reviewer Michael J. Ross
ISBN 1932394753
summary A pragmatic guide to object-oriented PHP development.
Authored by Dagfinn Reiersøl, with Marcus Baker and Chris Shiflett, the book was published on 3 July 2007 by Manning Publications, under the ISBNs 1932394753 and 978-1932394757. Its subtitle accurately reflects the major themes of the work: creating PHP applications built upon objects, utilizing Web-oriented design patterns, and incorporating agile programming techniques such as refactoring and test-driven development. Also covered are methods for effective form handling, database extraction, date and time representation, and more.

As a result of trying to adequately cover such a large number of major topics in a single book, the amount of material is considerable, and the book is certainly longer than the typical Web programming book in general, and PHP book in particular. Spanning 552 pages, the material is organized into 21 chapters, grouped into four parts: In Part 1 ("Tools and concepts"), the authors discuss PHP 5, its strengths and weaknesses, and how well it can be used with advanced programming principles; an overview of objects, exception handling, and references; visibility, abstract classes, and interfaces; effective use of classes and object-oriented design; inheritance, composition, and more on interfaces; advanced object-oriented principles; six design patterns that are especially appropriate for Web-based systems (Strategy, Adapter, Decorator, Null Object, Iterator, and Composite); and lastly, date and time handling using objects.

For developers well-versed in OOP, Part 1 may be more of a review, while Part 2 ("Testing and refactoring") could be the most valuable portion of the book. In the four chapters, the authors dig into the details of test-driven programming, refactoring, and Web testing. These chapters and all that follow take a very pragmatic approach to conveying ideas, which is consistent with the theme of Manning's "In Action" series, based upon the idea that programmers tend to learn best by reading sample code instead of generic discussion. For instance, test-driven development (TDD) is demonstrated by showing how to implement database transactions, a contact manager, and e-mail functionality. Mock objects and top-down testing are illustrated through the creation of an e-mail class, and further extended with a discussion of faking the mail server. Given that testing is the primary theme of the entire part, one might expect a more lengthy discussion of TDD, but Reiersøl correctly notes that this particular book is not trying to replace the many manuscripts and articles already published on agile development; also, the database examples adequately demonstrate the general principles discussed prior. The chapter on refactoring is well worth reading, and touches upon the controversial topic of how much one's PHP code should be separated from the HTML code — a topic later revisited in the chapter on templates. Also explored is a topic critical to maintenance programmers — refactoring versus rewriting. Two different testing frameworks are discussed, PHPUnit and SimpleTest; the latter is used throughout the book. The final chapter in this part explains how to test Web pages programmatically, by faking interaction, and other techniques. The chapter ends with a section providing steps on how to deal with "the horror of legacy code," when the unfortunate programmer has inherited a nightmare of a live Web site.

The third part of the book, "Building the web interface," begins with an examination of templates, the arguments for and against them, and three of the most commonly used template engines: Smarty, PHPTAL, and XSLT. One of the previously discussed design patterns, Composite, is utilized for combining templates to create complex Web pages. The chapter on user interaction makes use of the Model-View-Controller architecture, with the subsequent chapter delving deeper into the topic of controllers for Web pages. The next two chapters cover an area of site development that is a frequent cause of uncertainty, "bandage coding," and security risks: user forms and input validation. The book's coverage of the PEAR package HTML_QuickForm, alone makes it worth reading. Part 3 concludes with a chapter on abstracting database resources through objects and the Singleton pattern.

The fourth and last part of the book ("Databases and infrastructure") is relatively brief, comprising two chapters on marrying SQL with object orientation. The authors present a number of techniques for shoehorning SQL transactions into object-based code, including encapsulating queries inside of methods, building SQL statements dynamically, substituting SQL elements such as column and table names, using SQL aliases, and using SqlGenerator.

It is clear that the lead author, Dagfinn Reiersøl, has put a tremendous amount of time (three years, as noted in the preface) and effort into creating this work. The discussions are wide-ranging and in-depth, and there is just enough sample code to illustrate the ideas being discussed and also break up the visual monotony. The illustrations are limited in number, and consist mostly of class diagrams and UML sequence diagrams. Overall, the treatment of each topic clearly reveals that he has considerable experience with them, and has given thought to the pros and cons of some possible approaches, though not all of them.

However, there are still some weaknesses in the book. For example, in all of the material discussing how to separate the SQL code from the PHP code, I found no mention of stored procedures, such as those made possible in MySQL. All of the sample code appeared to be solid, though there was no clear reason for the inconsistent use of print() versus echo() is different code samples. All of the chapter summaries could be excised without any loss of value, and many of the chapter introductions could be eliminated as well or condensed.

On a more mechanical level, the book had many minor weaknesses: It was not encouraging to see the first erratum even before reaching page 1: "raising own level" on page xix, in the second paragraph. Readers may initially be confused by such attributions as "Uncle Bob [Uncle Bob]" (on page 77). In a future edition, it should be explained that names in square brackets are biographical references listed in the Resources section, which follows Appendix B. In the first sentence in Chapter 12, the reference to "Jackass" will probably be confusing to many readers — particularly non-Americans — and is not in the best of taste. In the text and the table of contents, the chapter and part titles are written in sentence case, instead of title case, for no obvious reason. It is not clear whether this is meant as an unsuccessful attempt at literary hipness, or just an unfortunate reflection of the current text messaging generation, which is eschewing rules of grammar that for centuries have made text easier to read. Finally, there was one problem in the production of the book, and not its writing: Several of the pages had light brown spots on them that were apparently part of the paper, and not a result of post-production staining. But these may be limited to my particular (brand-new) copy of the book.

Readers interested in learning more about the book could start at the publisher's Web page, which features an online table of contents and index, all of the book's source code, two sample chapters (7 and 21) in PDF format, and a link for purchasing the electronic version of the book, also as a PDF file. Any road/code warriors who do development on their laptops, on the go, will appreciate having this book readily available.

Yet most of these objections are minor and easily fixable, and do not detract from the value of this book. I especially liked the depth of experience brought to each topic, and the authors' consideration of differing viewpoints. PHP in Action is a competent, engaging, and detailed discussion of object-oriented and agile programming principles that can help PHP developers boost their effectiveness and the quality of their code.

Michael J. Ross is a Web developer, writer, and freelance editor.

You can purchase PHP in Action: Objects, Design, Agility from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
This discussion has been archived. No new comments can be posted.

PHP In Action: Objects, Design, Agility

Comments Filter:
  • PHP5 (Score:5, Interesting)

    by corychristison ( 951993 ) on Wednesday January 23, 2008 @03:41PM (#22157224)

    But with the released of version 5, PHP introduced a robust object model, and made it easier for its proponents to create well-architected Web sites and applications.
    ... PHP 5 was released ~2.5 years ago. It MUST be noted that there were some significant changes in the 5.2.x branch. I don't see any mention of PHP 5.2 in this review. This book is already dated.

    I do web development & design for a living. I use PHP because it is what I got into and I have not yet had the time and/or drive to really try anything else. PHP is so available and that is its real strength. It's biggest problem is those lazy folks who are still running 4.2.x or some branch that is or is to be discontinued very soon here. As well there are some known exploits and issues in the older branches.

    I love the rapid-ness of PHP though. At present I even use PHP-GTK2 to prototype all of my idea's while I learn new languages. That is, I'll make a rough draft in PHP-GTK and then try to do the same in C/C++... which is much more painstaking for someone who has used web development for so long. But I am slowly un-learning my habits to depend on magic to handle memory for me, etc.

    Yup. Just my random bablings.. I am sure there was a reason I started writing this comment.
  • Re:Stuck? (Score:5, Interesting)

    by LuckyStarr ( 12445 ) on Wednesday January 23, 2008 @04:10PM (#22157714)

    I've been using PHP for eight years and there hasn't been a day where I wished I had chosen another language. Sure, I wish the PHP of today was available eight years ago, but I can't complain with what is available now.
    That makes me wonder what other languages beside PHP you know.
  • by nuzak ( 959558 ) on Wednesday January 23, 2008 @04:32PM (#22158104) Journal
    I remember the the discussion on IRC that lead to the creation of the === operator. The designers of the language (back when they hung out on IRC) actually did not comprehend the concept of "object identity". I had to eventually resort to calling it a pointer compare, at which time I was told "PHP isn't C, it doesn't have pointers". That was pretty much the end of PHP for me.
  • by bigtrike ( 904535 ) on Wednesday January 23, 2008 @05:25PM (#22158946)
    My problem had nothing to do with destructors. There are other contexts where they can't be used. But of course you knew that, even though they're not really documented.

    function a() { new DateTime('2007-02-32'); }
    function b() { a(); }
    register_shutdown_function('b');

    And even then my complaint isn't that you can't do that, but that there is absolutely no way to track down the source of that error because unlike most other PHP errors, you're told it's on line 0 of Unknown.
  • by ChronoFish ( 948067 ) on Thursday January 24, 2008 @09:50AM (#22165944) Journal
    Maybe I'm just lazy,

    I'll accept that. I grew up programming in Pascal and BASIC, turned to C/C++ and loved it through college and my first 8 years of my software development career, dabbled in Java for a year - and then found PHP. For the last 5 years I have been programming in the language of my choice - PHP - and loving it. It's quick, it's easy, it's powerful. I find it just as easy to write a web server (Yes you can write a web server in PHP - the networking and forking functions are great - I've even successfully written apps that use COM to manipulate Excel) as it is to write a web app.

    Maybe my past experience allows me to write structured code without having the language force me to write structured code. Maybe my ideas of OO is backwards - I look at data as "black data" and functions "just handle it" rather than forcing the programmer to know what the data is in a strictly typed language (C++/Java).

    If you *want* to handle memory allocation - yeah - C/C++ is for you. But when you do that you're solving programming problems. You're not solving the customers problems. When you're a software developer you should be developing solutions for your customers - not solutions for the language you're developing in.

    Are there issues with PHP? Of course. Is it THE language to use? Only if you know it and it fits the problem you're trying to solve.

    -CF

"I've seen it. It's rubbish." -- Marvin the Paranoid Android

Working...