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.
Out of curiousity how many people are using PHP5? My hosting still only supports 4.3.something. And I'm still doing my testing strictly on 4.x to match current hosting options.
I tried using PHP 5 with my web host. It was a little bit slower, probably because version 5 was compiled as a CGI instead of a module. I am sure a comparison of both as modules would have been pretty close, if not dead even. Anyway, everything worked fine. From what I can tell version 5 is good for backwards compatibility.
24 beers in a case, 24 hours in a day. Coincidence? I think not!
But 5 will come eventually. In the "Me TOO" world of web hosts, once one goes over , they'll all have to.
That or they'll just when cpanel/plesk or what ever the contol panel of the day supports it..
We were in the process of upgrading to 5 until we basically saw no good reason to. The annoyances we found with 4 are still present in 5 and the OOP additions seem completely irrelevant or steps backwards; constantly requiring $this->... inside methods and the use of "__construct" didn't seem like the right direction to move in; as a result 4 will be our standard until we have no choice.
Debian still has no PHP 5 support, that probably helps. I am not sure why. Anyway, will hold out until PHP 5 goes .deb. The only thing I install from source on my servers is the Linux-kernel.
HTML Power Programming
BASIC Operating System Design
Enterprise AppleScript Applications
Kernal Programming With Javascript
Cool, where do I go to download my free copy?
http://people.debian.org/~dexter/dists/php5/woody/
Works great.
The man who trades freedom for security does not deserve nor will he ever receive either. - Benjamin Franklin
This has been the most in-depth PHP book I've found to date. It covers alot of advanced web development techniques as well as good software design practices. "Advanced" PHP literature is hard to find, but this book does a solid job of providing it.
"The best way to be ready for the future is to invent it"
So by PHP's reckoning.....
1) Establish PHP
2) "Invent the future"
3) ????
4) Profit!/Use your hovercar for daily commute
one of the the primary reasons that things like PHP and Perl constantly change is so that guys like this can keep writing books and running training classes?
"I'd rather be a lightning rod than a seismometer." -Ken Kesey
Isn't that what Debian is all about - waiting ..... and waiting ..... and waiting?
At the moment, not many shared web hosts support PHP5. I know that recently I bought "PHP5 and MySQL Bible" and learned about the many of the new features, but I was dissapointed to find out that a lot of the hosts out there right now do not support the new version.
For now, you will either have to scavenge the internet to find a host that supports it, or wait until it becomes more mainstream.
Another reason I believe that many hosts are not making the switch is that many popular PHP software has not been upgraded to support PHP5. phpBB, InvisionBoard, and the like all support PHP4, but have mainly 3rd party versions for PHP5. Why make the switch when most do not support the updated version?
(By the way, first post!)
After speaking with many hosts, they won't upgrade because their hosting package (ie: cpanel) doesn't support it yet. And they won't upgrade until that happens. The problem with cpanal supporting it is that some of the scripts they bundle (namely osCommerence) don't work in PHP5.
I've also found a lot that people still believe PHP5 is in beta and not compatible with PHP4 code. But here's the good word on the situation from the PHP folks.
Get off yer high horse fer chreissake. So you're a REAL PROGRAMMER who probably drinks REAL ALE, wears a DEBIAN t-shirt and can recite LORD OF THE RINGS backwards. Some of us, meanwhile, are trying to learn to programme and this book, in case you didn't read the review, is considered best of breed.
Seems to be really good, I think that fact that one of the authors (Zeev), who invented/develops the Zend engine speaks for itself.
Covers lots of things even the basics, has some really good advice on form security and session security and explains how the hackers actually attempt to exploit loopholes in your forms and session data and how to get around this.
Has a guide to generating bar charts/graphs which I found really usefull. Has good info on XML and SOAP, SQLi and multiple queries.
It tends to have quite a beginners approach, but I'm not discrediting the book and dont let this put you off, the subject matter and topics covered make it more than worthwhile for php professionals and there is a wealth of information for migrators looking to use the new php5 (eg OOP) features.
...bravo for using the word "adroitly". Don't see that one too often. :)
I'm not good in groups. It's difficult to work in a group when you're omnipotent. - Q
I hadn't ever done any web programming before purchasing this book (about 2 months ago), but had plenty of experience with C++ and OO design. This book was perfect for me; it showed me how to do all the OO stuff I wanted to in PHP, and gave enough information about using sessions, post and get data etc to get started. After a week of tinkering I was off to the races!
That said, I don't think this book would really do a good job showing someone how to use the OO features effectively who had never been exposed to it before, and that's probably fine. Just don't expect its half ass coverage of design patterns to give you a whole lot if you haven't scene them before.
-K
PHP5 really is a nice step forward from PHP4. It's improved object support and exception handling make it easier to sell as a "real language" with management and clients. And PDO looks quite promising as well. Unfortunately, it is hard to find hosts that support PHP5.
I've put together a set of componenets that I developed under a loose framework for PHP5, called phacade , that provide some of the conveniences of ASP.NET and JavaServer Faces, without all the headeaches those two technologies seem to include. phacade aims to provide components that can be used simply for simple projects, and as simply as possible for complex projects. Much like PHP itself, phacade is intended to work equally well for both procedural and Object Oriented scripts.
If you're a PHP5 developer, please check it out and feel free to send feedback.
Anyone else have any interesting PHP5 projects?
Litmus Test @ Monster.com
I try to apply that Test when I feel dumb for putting stuff on my resume'.
no. That's Gentoo. Oh and Windows.
/* oops I accidentally made a comment, sorry */
I do a lot of DOM / XSLT programming for freelance and for my job. I just refuse to use PHP4 when I don't have to do so. DomXML in PHP4 used functions in order to return objects.
E.g: $node->first_child();
Therefore, it was impossible to go:
E.g: $node->first_child()->attributes;
In PHP5, it's simply:
$node->firstChild->attributes;
I believe in PHP5, it's possible to call the method of an object returned directly ($obj->func()->func2()).
The upgrade from PHP4 to PHP5 is nowhere as painful as from PHP3 to PHP4. In fact, the only errors I ended up logging when I transitioned over were my own coding errors that didn't show up in PHP4 ($this->item was being referenced outside of a class).
I'm gonna be loading the MySQLi module on there when I get the chance. I've really been itching to do stuff like MySQLi::prepare outside of Pear:DB. Not to mention, being able to use SSL to connect to MySQL from PHP is a definate plus.
/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
Right here...
http://www.phptr.com/series/series.asp?ser=335494
Well, this is the main page. There's a link just after the intro.
PHP5 sounds really great. I'd like to use it, really I would. But I am so spoiled by ColdFusion, especially the excellent database support.
I was *shocked* when I found out PHP projects are coded for a specific database. In Coldfusion I can change database vendors merely by making a change on an admin page. All the Coldfusion code remains the same. Of course of you have to avoid non-standard SQL inside your queries.
Is PHP database support anywhere near Coldfusion's?
Not trolling, just would like to know.
- For the complete works of Shakespeare: cat
Say what you want, I've been getting job offers and phonecalls from recruiters looking for contractors to do LAMP projects, specifically in PHP. Most people are attracted to the virtually ZERO cost and crapload of free tools.
For instance, I was actually asked to do an application for a client where scripting language and platform hadn't been decided. the mini-app had to generate images on the fly, include a search-engine style capability, etc. In ASP-land, I'd have to use an external DLL for the images and buy a searching script. In PHP-land, I used the built-in graphics library and phpdig. Although I think ASP.NET has its own built in graphics libraries, its simply much much harder to find free code for it. I know that might be an anathema to some, but why re-write barcode generating scripts or search functionality when a good alternative has already been written?
Reason, free market capitalism, and individualism
I haven't read the book, and I don't intend to, but I do think the review gives a rather good assessment of what the shift to PHP 5 means in practice.
I am personally of the conviction that OO is among those things that are absolute necessities to keep the work of mediocre (sloppy) programmers like myself moderately sane and structured, as soon as the number of lines exceeds throwaway level, say, 10,000 lines or more. I can't read my own code, let alone that of somebody else, if it's more than 10,000 lines and doesn't have a good class diagram as its basis, unless some other methodology was used, and those other methodologies are more problem specific than OO.
Now I've seen (and luckily enough, managed to largely stay out of participating in) a project that was done in PHP4. Honest attempts were made (I swear) to separate user interface from business logic and to implement the latter in terms of an OO class design, but PHP wouldn't even let us *pass references as function arguments* without complaining. Which meant the OO support that was there was little more than shallow imitation. Which basically meant we couldn't implement the class design because the language wouldn't let us.
As you may have guessed by now I'm also a fervent advocate of strong, static typing so I wouldn't trust myself with PHP 5 anyway, but it's good to know that they have at least managed to pass the hurdle of knowing what a reference is. It is (truly!) remarkable to see the power of open source in PHP, which has grown from a dirty hack of a Perl script to improve upon the even worse hack of Apache SSI, into a full-blown programming language, and I'd regard it as past its adolescence. The reviewer rightly points out XML support as a particular area in which previous versions of PHP lacked maturity. So it's getting there, but if you ask me: where exactly, I'll reply: where Java, NET and possibly some other languages are today.
Why is the parent modded flamebait. S/he is telling the truth about PHP on IIS/Windows. It does have terrible performance compared to Linux/Apache. And it is not because of IIS or windows either. PHP, runs as an cgi on Windows, because of stability issues with the PHP ISAPI plugin.
Obviously all OO has a "this" (or similar) for self-referencing, however PHP5 *requires* its use for referencing class variables; it seems silly to always have to be explicit when standard scoping rules would apply.
You mean having a class initiate at the start without having to call upon it? Oh no! That's definately a step in the wrong direction.
It's obvious that a constructor is required for a class, I'm pointing out that it's silly to use __construct for the method name rather than the name of the class (as is convention in just about every other OO language and was what PHP4 used)
The reason __construct was used is so you can call the parent constructor without having to know it's name. This makes it easier when refactoring code.
An oxymoron