phpdao

PHP DAO/VO Generator

By Richard Beton

phpdao is a tool to generate Data Access Objects and Value Objects for PHP4/MySQL web applications. This greatly improves the readability of PHP scripts that use a MySQL database.

The DAO and VO technique is widely used in Java web applications, but PHP webapps surprisingly usually overlook the benefits they bring. That's why phpdao was created.

What's the Benefit?

  1. All the SQL statements used by your application can be in a single layer of PHP classes. Therefore, there is greater isolation from your database layer.
  2. Your PHP application code will need no SQL statements of its own. Therefore, your PHP code is simpler and clearer.
  3. The Value Objects make it obvious what each table row contains.

The tool consists of three elements:

  1. A Python script: this generates PHP code from an SQL schema. The schema must be in a fixed format; at present, the format as produced by phpMyAdmin is supported.
  2. A PHP class: this is for wrapping SQL calls. This is used by the generated PHP classes. You are not required to use it, but it provides some debugging facilities.
  3. Example code illustrating possible usage of the tool.

How you use it:

  1. Get your table schemas, e.g. by exporting from phpMyAdmin to a text file.
  2. Run phpdao's writeVo.py script to create the VO and DAO classes.
  3. In your PHP pages:
    • Include the VO/DAO classes,
    • Create DAO instances and call the database access methods.

Extension:

The DAOs are generated as the sum of the automatically-generated PHP plus any manual additions you need to include. These might wrap up special SQL statements in the DAO layer for example. Or they may provide caching or dictionary functions or whatever.