As a web programming language, one of PHP’s strengths traditionally has been to make it easy to write scripts that access databases so that you can create dynamic web pages that incorporate database content. This is important when you want to provide visitors with information that is always up-to-date, without hand tweaking a lot of static HTML pages. However, although PHP is easy to use, it includes no general-purpose database access interface. Instead it has a number of specialized ones that take the form of separate sets of functions for each database system. There is one set for MySQL, another for InterBase, and another for PostgreSQL—and others as well.
This wide range of support for different database engines help make PHP popular because it means essentially that no matter which database you use, PHP probably supports it. On the other hand, having a different set of functions for each database also makes PHP scripts non-portable at the lexical (source code) level. For example, the function for issuing a SQL statement is named mysql_query(), ibase_query(), or pg_exec(), depending on whether you are using MySQL, InterBase, or Post-greSQL. This necessitates a round of messy script editing to change function names if you want to use your scripts with a different database engine, or if you obtain scripts from someone who doesn’t use the same engine you do.
In PHP 4 and up, this problem is addressed by means of a database module included in PEAR (the PHP Extension and Add-on Repository). The PEAR DB module supports database access based on a two-level architecture:
• The top level provides an abstract interface that hides database-specific details and thus is the same for all databases supported by PEAR DB. Script writers need not think about which set of functions to use.
• The lower level consists of individual drivers. Each driver supports a particular database engine and translates between the abstract interface seen by script writers and the database-specific interface required by the engine. This provides you the flexibility of using any database for which a driver exists, without having to consider driver-specific details.
This architectural approach has been used successfully with other languages—for example, to develop the DBI (Perl, Ruby), DB-API (Python), and JDBC (Java) database access interfaces. It’s also been used with PHP before; PHPLIB and MetaBase are two packages that provide an abstract database interface. Howev er, PEAR is included with PHP distributions and installed by default, so if you have a recent version of PHP, you already have PEAR and can begin using it.
PEAR DB uses classes and objects to present an object-oriented interface, and this article assumes that you are familiar with PHP’s approach to object-oriented programming. If you are not, you may wish to review the ‘‘Classes and Objects’’ chapter of the PHP Manual.
The PEAR DB architecture implements database support primarily through two files that are used for all database engines, and a third that is chosen on an engine-specific basis:
• The primary ‘‘overseer’’ file is DB.php. It implements the DB class that creates database connection objects, and also contains some utility routines.
• DB/common.php implements the DB_common class that forms the basis for database access.
Download pdf Writing Scripts with PHP’s PEAR DB Module
Related Searches: web programming language, pear db, dynamic web pages, messy script, source code level
RSS feed for comments on this post · TrackBack URI
Leave a reply