Slashdot Mirror


Advanced PHP Programming

sympleko (Matthew Leingang) writes "PHP5 has hit its third release candidate, so get used to the idea of using it. George Schlossnagle has written a great book on PHP programming which ought to generate some enthusiasm. But it's not just about PHP5: the book includes great information on everything from coding style to high-level problem-solving. I met George through a friend of mine who works for the Developers Library, and I'm glad to have finally gotten a look at his book." Read on for Leingang's review of Advanced PHP Programming: A practical guide to developing large-scale Web sites and applications to PHP5. Advanced PHP Programming: A practical guide to developing large-scale Web sites and applications to PHP5 author George Schlossnagle pages 609 publisher Sams Publishing (Developers Library) rating 9.999/10 (I don't give perfect scores) reviewer Matthew Leingang ISBN 0672325616 summary See subtitle

Many of the previous generation of PHP books were fattened with lots of filler: how PHP imports form-submitted variables into its namespace, definitions and examples of valid XML documents, one-line summaries of every PHP function, even an HTML reference. It's like going to Gallagher's Steak House and filling up on free bread. Ladies and gentlemen, may I submit the Atkins-Friendly PHP book. This is not a book about syntax or data structures. This is a book on how to use PHP in enterprise environments. During my first read, I realized around page 126 that I had already learned as much as I had expected to learn and I was just getting started!

The book is very well written, with a friendly tone that is neither pedantic nor partisan. A knowledge of PHP before version 5 is assumed, and the situations tackled are very much from the real world. The focus goes beyond getting what you want to appear in the browser, too; scaling problems of very large web sites, managing a code base with multiple developers, and building your own extensions to PHP are all discussed.

The author draws most examples from a Unix + Apache + PHP environment, and MySQL is the primary database used. The examples are all in PHP5, but many ideas can still be implemented in PHP4. In other words, you can still learn a lot even if you're committed to PHP4 for the near future.

Part I of the book is called Implementation and Development Methodologies (some of these part and chapter names could be a little less clunky, even if they are correct), and the first chapter is about coding style. After that comes a thorough discussion of the new features of PHP5. These are language aspects that are commonplace in other object-oriented languages (e.g., java and python), but which I admittedly knew little about:

  • encapsulation: the ability to keep object attributes and methods private or protected;
  • static attributes and methods to make class functions or singletons;
  • user-definable constructor, destructor, accessor, mutator, and copier functions;
  • interfaces, which are like abstract classes. A class can implement one or more of these as well as extend a concrete class;
  • exceptions, which allow propagation of errors and warnings back up through the function stack.

Other PHP programming concepts are discussed in this part, such as templating, using the command-line interface, and unit testing. The chapter on Managing the Development Environment includes some CVS basics as well as how to organize and keep separate your development and production environments without breaking what works. Another topic discussed is when to use PEAR classes and when to roll your own.

Part II, Caching, is where the book gets hard-core. Once your application works, how do you optimize its performance and scale it so you can have hundreds of thousands of users?

  • You can use static variables to reduce recalculations, and compile your regexps.
  • You can cache data on the PHP level in flat files, DBM files, shared memory, or even in user cookies.
  • There are also solutions outside of the PHP userspace. The ordinary PHP process takes the text which constitutes the programmer's code and compiles into a assembly-style intermediate code. Then the intermediate code is executed. If a script is executed several times without change, the same intermediate code is created, executed, and thrown away several times. A compiler cache saves this intermediate code and reuses it. The author has developed a free, open-source compiler cache.
  • There are also code optimizers, which eliminate dead code and overly-verbose constants.
  • Reverse proxies also work in web sites to reduce network latency. Latency occurs when a server is stuck waiting for a request to be completed before it can execute. A reverse proxy server only collects requests, then hands them off on a high-speed network to the actual server.
  • This can be made even better with content caching. The proxy can determine if the request requires handling by the PHP webserver at all. If a stale, cached copy suffices, it is served instead.
  • Content compression sends your data over the internet compressed. The client's browser is in charge of decompression.

Part III, Distributed Applications, is of big importance to the developer of medium-sized sites. The author discusses the familiar topics of database interaction (including how to troubleshoot your slow queries), authentication, and session handling. Then a chapter on clustering: how to arrange multiple, redundant servers to create a robust, fail-safe system.

The final chapter in this part covers another hot topic: Web Services. Say you want to edit your weblog entries on a real text editor rather than through a web form. If you have RPC (Remote Procedure Calls) set up on your web server, you need only write a script which manufactures a request to a web service and and ships it out. What's the sales rank on Amazon of the book you just wrote? What's the weather like in Medford, Massachusetts today? These are all jobs for web services. XML-RPC and SOAP are approaching the standards state of usage, so using one of these means you don't have to develop your own RPC client or server library. SOAP is even richer than XML-RPC: it's an all-purpose messaging protocol which is in use by many of the big players in web services (e.g., Amazon, Google).

In Part IV, Performance, the author returns to the optimization question. How can PHP scripts themselves be made to run faster? There are several techniques:

  • Use the apache benchmarker or other load-generating programs to determine which requests take the most time.
  • Use a PHP profiler such as the one the author has written to examine your script line-by-line and determine which function calls are most expensive.
  • Use a synthetic benchmarker (such as the one included in PEAR) to analyze small bits of code and discover how efficiently they do their task. Which is faster: interpolation of variables or string concatenation (the latter, at least before PHP 4.3)? If you don't have a library compiled into PHP, can you implement all the functions in userspace efficiently (not really)?

Part V, Extensibility, is for people who want to adapt PHP on the language level for their needs. This part requires a knowledge of C and a strong grip on your hat! After a discussion of PHP and Zend Engine (the virtual machine on which compiled PHP runs) internals, the author shows how to make both simple and complex extensions. You can add new functions to PHP, add a suite of library wrappers, add and manipulate classes and objects, all using pre-defined macros. In the last chapter, you can extend Zend itself to (say) implement all errors as exceptions, create a PHP Shell, an opcode-dumper, or modify the author's compiler cache or profiler.

I very much enjoyed the book. I have chosen to take the plunge into PHP5 for a new web project, partly because this book convinced me it's worth it. I can't imagine I'm going to use everything I've learned from the book, but I'm glad to know how problems like these are solved.

There are a few typos and misspellings, but that's to be expected in such a large book with limited turnaround time. Definitely recommended.

Matthew Leingang is a Preceptor in Mathematics at Harvard University. He continues to try to integrate web development into his day job. You can purchase Advanced PHP Programming: A practical guide to developing large-scale Web sites and applications to PHP5 from bn.com. Slashdot welcomes readers' book reviews. To see your own review here, carefully read the book review guidelines, then visit the submission page.

11 of 189 comments (clear)

  1. most of the books by joeldg · · Score: 4, Interesting

    most of the books don't cover the topics of cli-based php which is unfourtunate.
    Things like php-ncurses and php-gtk or even how to properly debug cli apps and it is strange saying there is a large following of PHP commandline (cli) people out there.

    Anyway, always good to see more books at any rate.

    1. Re:most of the books by name773 · · Score: 2, Interesting

      and don't forget sockets!
      i wrote an encrypted chat client/server combo using php exclusively with ncurses and sockets
      source is here

      it's a bit hard to setup, because it was just a for phun project... man php is great

  2. Re:php-embed by SIGALRM · · Score: 4, Interesting

    It's not really a mod, just a php_embed.c and php_embed.h which wrap up some TSRMLS functions, not much documentation on it, but a decent thread here.

    --
    Sigs cause cancer.
  3. Latest software by Ford+Prefect · · Score: 5, Interesting

    PHP 5? Great!

    I've got a bit of a complaint about computer books, in that they frequently concentrate on the latest and greatest versions and brush aside the older versions - the versions that the majority of web hosts might be running, for instance.

    I was looking for a MySQL book a while back, and there were dozens of them on the shelves in the bookshop, but all based around MySQL 4 - not the ubiquitous MySQL 3 that I was trying to learn. Looking through at all the new features can be a bit dispiriting, especially when you're stuck with the older version.

    Anyone else had similar problems?

    --
    Tedious Bloggy Stuff - hooray?
  4. Re:Mysql + Apache 1.x by CHaN_316 · · Score: 3, Interesting

    I think your argument is just a matter of opinion. One could have easily asked

    Sigh, I really wish these books would get with the times and start using SQL Server 2000 (or even Oracle), and Microsoft IIS

    I'm a firm believer of using the right tool for the right job. Some projects I've worked on don't really need foreign key enforcement, and subqueries, and what not provided by PostgreSQL. MySQL has worked just fine in a lot of cases. Some industrial strength applications may need a PostgreSQL. But whatever makes the most sense should be used.

    --
    "There is no spoon." - The Matrix
  5. Advanced PHP by Deltawolf · · Score: 3, Interesting

    This book seems like a friendly addition to the many 100s of php books already out there. I have bought many php books and they all seem to go over the same thing, syntax and functions. But this book from the description seems to do more than simply explain functions and syntax but also usability and practical use. I am a php programmer myself and much of the problem of coding programs is high volumes of data and usage. By using a code cache system outlined in this book it would definatly allow many good programs that are out today to be used on a larger scale. I know a bulletin board I coded a year or two back could have used this, it choked at 1000 or so users at once. PHP5 is coming and it looks to me like a force to be reckoned with. While you naysayers who code perl, c, and other languages put down PHP as a pathetic language, PHP5 should be getting on par with perl and other languages.

    --
    -Rights? What rights?
  6. Re:Mysql + Apache 1.x by rho · · Score: 1, Interesting
    pgsql fulfills certain needs better than mysql. I prefer pgsql, because I have a lot more experience with it. Often, pgsql is touted for its ACID compliance, which is (usually) unneccessary for Web applications.

    mysql always seemed to me to be a real amateurish production, while pgsql seemed more professional. That's just interpretation, however, and may be influenced by the fact that every half-assed Perl hackery uses mysql as its backend. Guilt by association, I suppose.

    If I were doing something that needed a certain degree of robustness, however, I'd definitely go for pgsql. I'm less terrified of it.

    --
    Potato chips are a by-yourself food.
  7. Maybe around 5.3 by electricdream · · Score: 2, Interesting

    I'm thrilled to death that PHP is finally starting to catch up with the rest of the world. A solid XML interface, SOAP support, and OO like behavoir are great steps toward a modern programming language. However after my experience in with the PHP 4 I won't be upgrading anytime soon. Can't deal with the constant configuration changes, changing function behavoirs, bugs, and generally moving target of a language.

    Don't get me wrong I love the current state of PHP for developing small fast websites. But if history is a good teacher then PHP 5.3 will be the release which should have been called PHP 5.0. Good luck to those adventurous souls who will be running 5.0 on production servers. I hope you can tell me the dev community has grown up and stablized their dev cycle and that I can upgrade at 5.1 instead of 5.3

    --
    -- force and mind are opposites; morality ends where a gun begins ayn rand
  8. Re:php-embed by Scaba · · Score: 2, Interesting

    Just on a guess, I'd say there are a lot more PHP developers out there than Lua or Ruby developers (though Lua looks pretty cool). Besides, Ruby developers are all just ex-Pythoneers who thought Python was becoming too popular, and don't really count as developers anyway ;>)

  9. Re:php-embed--troll/rant by Fweeky · · Score: 2, Interesting

    What features does PHP have which make it especially suited to form and database interfaces? Many languages have easy to use CGI/FCGI/Server API's; many have HTML-embedded versions and high quality template libraries; many have database libraries which easily rival and surpass those of PHP, and many have fancy dynamic form generation and processing libraries.

    PHP's far from best-of-class in any of these respects in my experience; only pervasiveness is in it's favour, and running my own servers, that isn't much of a conern to me.

    Give me an example of how easy it is to interface a form with a database, complete with validation and listing using PHP in ways other languages can't match. In fact, show *anything* which you can do more cleanly in PHP than any other language, and you'll be on your way to having a decent response to the grandparent poster.. and me ;)

  10. Re:Mysql + Apache 1.x by scrytch · · Score: 2, Interesting

    > Some projects I've worked on don't really need foreign key enforcement, and subqueries, and what not provided by PostgreSQL

    MySQL not supporting foreign keys is just fine. I just wish it would stop PRETENDING to support them by parsing and subsequently IGNORING foreign key declarations.

    MySQL's documentation has typically heaped scorn on everything they didn't implement that version, then the attitude suddenly disappears when they do get around to implementing it. It strikes me as an astonishingly unproductive attitude, to say nothing of unprofessional. Go grab an old mysql rpm (with docs) or grab the docs out of the wayback machine if you want to see for yourself.

    Incidentally, MySQL's speed comes mostly when using the MyISAM driver. Hope you weren't wanting a database larger than 2 gigs. I can and do fill up that much in a day with my postgresql db.

    --
    I've finally had it: until slashdot gets article moderation, I am not coming back.