Tuesday, January 6, 2009

An Introduction to PHP - PHP 4 Features


(Page 2 of 4 )

On May 22, 2000, roughly 18 months after the first official announcement of the new development effort, PHP 4.0 was released. Many considered the release of PHP 4 to be the language’s official debut within the enterprise development scene, an opinion backed by the language’s meteoric rise in popularity. Just a few months after the major release, Netcraft (http://www.netcraft.net/) estimated that PHP had been installed on over 3.6 million domains, making it one of the most popular scripting languages in the world.

Features

PHP 4 included several enterprise-level improvements, including the following:

  • Improved resource handling: One of version 3.x’s primary drawbacks was scalability. This was largely because the designers underestimated how much the language would be used for large-scale applications. The language wasn’t originally intended to run enterprise-class Web sites, and subsequent attempts to do so caused the developers to rethink much of the language’s mechanics. The result was vastly improved resource-handling functionality in version 4.

  • Object-oriented support: Version 4 incorporated a degree of object-oriented functionality, although it was largely considered an unexceptional implementation. Nonetheless, the new features played an important role in attracting users used to working with traditional object-oriented programming (OOP) languages. Standard class and object development methodologies were made available, in addition to object overloading, and run-time class information. A much more comprehensive OOP implementation has been made available in version 5, and is introduced in Chapter 5.
  • Native session-handling support: HTTP session-handling, available to version 3.x users through the third-party package PHPLIB (http://phplib.sourceforge.net), was natively incorporated into version 4. This feature offers developers a means for tracking user/site interactions with unparalleled efficiency and ease. Chapter 13 covers PHP’s session-handling capabilities.
  • Encryption: The MCrypt (http://mcrypt.sourceforge.net) library was incorporated into the default distribution, offering users both full and hash encryption using encryption algorithms including Blowfish, MD5, SHA1, and TripleDES, among others. Chapter 16 delves into PHP’s encryption capabilities.
  • ISAPI support: ISAPI support offered users the ability to use PHP in conjunction with Microsoft’s IIS Web server as an ISAPI module, greatly increasing its performance and security.
  • Native COM/DCOM support: Another bonus for Windows users is PHP 4’s ability to access and instantiate COM objects. This functionality opens up a wide range of interoperability with Windows applications.
  • Native Java support: In another boost to PHP’s interoperability, support for binding to Java objects from a PHP application was made available in version 4.0.
  • Perl Compatible Regular Expressions (PCRE) library: The Perl language has long been heralded as the reigning royalty of the string parsing kingdom. The developers knew that powerful regular expression functionality would play a major role in the widespread acceptance of PHP, and opted to simply incorporate Perl’s functionality rather than reproduce it, rolling the PCRE library package into PHP’s default distribution (as of version 4.2.0). Chapter 9 introduces this important feature in great detail, and offers a general introduction to the often confusing regular expression syntax.

In addition to these features, literally hundreds of functions were added to version 4, greatly enhancing the language’s capabilities. Throughout the course of this book, I’ll discuss much of this functionality, as it remains equally important in the version 5 release.

Drawbacks

PHP 4 represented a gigantic leap forward in the language’s maturity. The new functionality, power, and scalability offered by the new version swayed an enormous number of burgeoning and expert developers alike, resulting in its firm establishment among the Web scripting behemoths. Yet maintaining user adoration in the language business is a difficult task; programmers often hold a “what have you done for me lately” mindset. The PHP development team kept this notion close at hand, because it wasn’t too long before they began yet another monumental task, one that could establish PHP as the 800-pound gorilla of Web scripting languages: Version 5.


An Introduction to PHP - PHP 5 Features


(Page 3 of 4 )

Version 5, in beta at the time of this writing, is sizing up to be yet another watershed in the evolution of the PHP language. Although previous major releases had enormous numbers of new library additions, version 5 contains improvements over existing functionality and adds several features commonly associated with mature programming language architectures.

  • Vastly improved object-oriented capabilities: Improvements to PHP’s object-oriented architecture is version 5’s most visible feature. Version 5 includes numerous functional additions such as explicit constructors and destructors, object cloning, class abstraction, variable scoping, interfaces, and a major improvement regarding how PHP handles object management. Chapters 6 and 7 delve into the subject of object-oriented PHP.
  • Try/catch exception handling: Devising custom error-handling strategies within structural programming languages is, ironically, error-prone and inconsistent. To remedy this problem, version 5 now supports exception handling. Long a mainstay of error management in many languages, C++, C#, Python, and Java included, exception handling offers an excellent means for standardizing your error reporting logic. This new and convenient methodology is introduced in Chapter 8.
  • Improved string handling: Prior versions of PHP have treated strings as arrays by default, a practice indicative of the language’s traditional loose-knit attitude towards datatypes. This strategy has been tweaked in version 5, in which a specialized string offset syntax has been introduced, and the previous methodology has been deprecated. The new features, changes, and effects offered by this new syntax are discussed in Chapter 9.
  • Improved XML and Web Services support: XML support is now based on the libxml2 library, and a new and rather promising extension for parsing and manipulating XML, known as SimpleXML, has been introduced. In addition, a SOAP extension is now available. In Chapter 18 I'll introduce SimpleXML and the new SOAP extension, and also demonstrate a number of slick third-party Web Services extensions.

  • Native support for SQLite: Always keen on choice, the developers have added support for the powerful yet compact SQLite database server. SQLite offers a convenient solution for developers looking for many of the features found in some of the heavyweight database products without incurring the accompanying administrative overhead. I introduce SQLite and PHP’s support for this powerful database engine in Chapter 20.

A host of other improvements and additions are offered in version 5, many of which I’ll introduce as relevant throughout the book. When practical, I’ll be sure to introduce these improvements as the book proceeds.

As the release of PHP 5 draws near, PHP’s prevalence is at a historical high. At press time, PHP has been installed on more than 15 million domains (Netcraft, http://www.netcraft.com/). According to E-Soft, Inc. (http://www.securityspace.com/), PHP is by far the most popular Apache module, present on over 50 percent of all Apache installations.

So far, I’ve only discussed version-specific features of the language. Each version shares a common set of characteristics that play a very important role in attracting and retaining a large user base. In the next section, you’ll learn about these foundational features.

An Introduction to PHP - General Language Features of PHP


(Page 4 of 4 )

Every user has his or her own specific reason for using PHP to implement a mission-critical application, although I find that such motives tend to fall into what I like to call the four pillars of PHP: Practicality, Power, Possibility, and Price.

Practicality

From the very start, the PHP language was created with practicality in mind. After all, Lerdorf’s original intention was not to design an entirely new language, but to resolve a problem that had no immediately applicable solution. Furthermore, much of PHP’s early evolution was not the result of the explicit intention to improve the language itself, but rather to increase its utility to the user. The result is what I like to call a minimalist language, in terms of what is required of the user, and in terms of the language’s syntactical requirements. For starters, a useful PHP script can consist of as little as one line; there is no need for the mandatory inclusion of libraries like in C. For example, the following represents a complete PHP script, the purpose of which is to output the current date, such as March 16, 2004:

Another example of the language’s penchant for compactness is its ability to nest functions. For example, I can effect numerous changes to a value on the same line by stacking functions in a particular order, in this case producing a pseudorandom string of five alphanumeric characters, such as “a3jh8”:

$randomString = substr(md5(microtime()), 0, 5);

PHP is a loosely-typed language, which also increases its practicality. For example, there is no need to explicitly create, typecast, or destroy a variable, although you are not prevented from doing so. PHP handles all of this for you, creating variables on the fly as they are called in a script, employing a best guess formula for automatically typecasting variables, and finally, automatically destroying variables and returning resources back to the system when the script completes. In these and many other respects, the language allows the developer to concentrate almost exclusively on the final goal (a working application), attempting to handle many of the administrative aspects of programming internally.

Power

In the earlier introduction to PHP 5, I alluded to the fact that the new version is more qualitative than quantitative in comparison to previous versions. Previous major versions were accompanied by enormous additions to PHP’s default libraries, to the tune of several hundred new functions per release. Presently, 113 libraries are available, collectively containing well over 1,000 functions. Although you’re likely aware of PHP’s ability to interface with databases, manipulate form information, and create pages dynamically, did you know that PHP can:

  • Create and manipulate Macromedia Flash, image, and Portable Document Format (PDF) files
  • Evaluate a password for guessability by comparing it to language dictionaries, and easily broken patterns
  • Communicate with the Lightweight Directory Access Protocol (LDAP)
  • Parse even the most complex of strings using both the POSIX and Perl-based regular expression libraries
  • Authenticate users against login credentials stored in flat files, databases, and even Microsoft’s Active Directory
  • Communicate with a wide variety of protocols, including IMAP, POP3, NNTP, and DNS, among others
  • Communicate with a wide array of credit-card processing solutions

Of course, I’ll strive to cover as many of PHP’s interesting and useful features as possible in the coming chapters.

Possibility

PHP developers are rarely bound to any single implementation solution. On the contrary, a user can easily be overwhelmed by the number of choices offered by the language. For example, consider PHP’s array of database support options. Native support is offered for over twenty-five database products, including Adabas D, dBase, Empress, FilePro, FrontBase, Hyperwave, IBM DB2, Informix, Ingres, Interbase, mSQL, direct MS-SQL, MySQL, Oracle, Ovrimos, PostgreSQL, Solid, Sybase, Unix dbm, and Velocis. In addition, abstraction layer functions are available for accessing Berkeley DB-style databases. Finally, two database abstraction layers are available, one called the dbx module, and another via PEAR, titled the PEAR DB.

PHP’s powerful string-parsing capabilities is another feature indicative of the possibility offered to users. In addition to more than eighty-five string manipulation functions, two distinct regular expression formats are supported POSIX and Perl-compatible. This flexibility not only offers users of differing skill sets the opportunity to immediately begin performing complex string operations, but also to quickly port programs of similar functionality (such as Perl and Python) over to PHP.

Do you prefer a language that embraces functional programming? How about one that embraces the object-oriented paradigm? PHP offers comprehensive support for both. Although PHP was originally a solely functional language, the developers soon came to realize the importance of offering the popular OOP paradigm, and took the steps to implement an extensive solution.

The recurring theme here is that PHP allows you to quickly capitalize on your current skill set with very little time investment. The examples set forth here are but a small sampling of this idea, which can be found repeatedly throughout the language.

Price

Since its inception, PHP has been without usage, modification, and redistribution restrictions. In recent years, software meeting such open licensing qualifications been referred to as open-source software. Open-source software and the Internet go together like bread and butter. Open-source projects like Sendmail, Bind, Linux, and Apache all play enormous roles in the ongoing operations of the Internet at large. Although the fact that open-source software is available free has been the characteristic most promoted by the media, several other characteristics are equally important if not more so:

  • Free of licensing restrictions imposed by most commercial products. Open Source software users are freed of the vast majority of licensing restrictions one would expect of commercial counterparts. Although some discrepancies do exist among license variants, users are largely free to modify, redistribute, and integrate the software into other products.

  • Open development and auditing process. Although there have been some incidents, open-source software has long enjoyed a stellar security record. Such high standards are a result of the open development and auditing process. Because the source code is freely available for examination by anyone who wants to, security holes and potential problems are rapidly found and fixed. This advantage was perhaps best summarized by open-source advocate Eric S. Raymond, who wrote, “Given enough eyeballs, all bugs are shallow.”
  • Everyone is free to participate. Development teams are not limited to a particular organization. Anyone who has the interest and the ability is free to join the project. The absence of member restrictions greatly enhances the talent pool for a given project, ultimately contributing to a higher quality product.
Summary

This chapter has provided a bit of foreshadowing about this wonderful language to which much of this book is devoted. We looked first at PHP’s history, before outlining version 4 and 5’s core features, setting the stage for later chapters.

In Chapter 2, prepare to get your hands dirty, as you’ll delve into the PHP installation and configuration process. Although readers often liken most such chapters to scratching nails on a chalkboard, you can gain much from learning more about this process. Much like a professional cyclist or race car driver, the programmer with hands-on knowledge of the tweaking and maintenance process often holds an advantage over those without, by virtue of a better understanding of the software’s configurable behaviors and quirks. So grab a snack and snuggle up to your workstation; it’s time to build.