phpfunk.com


Object Oriented Programming with PHP 5

Series of articles on object oriented programming with PHP 5.

PHP Open Source Library

We recently uploaded documentation for our PHP library. The API so far contains revised versions of our best base classes retooled for PHP 5. To date the most complete packages are DB (database abstraction) and Util (utilities).

Whats with the long class names?
One of the goals was to not hog the global namespace by naming each class following the standard VendorName_PackageName_ClassName.

PHP database abstraction layer for PHP 4

There is a PHP 5 version of this class in the DB package. The following references the PHP 4 implementation. The PHP 4 version is compatible with PHP 5.

A php database abstraction layer for PHP4
The DBWrapper object takes a driver. The driver knows how to interact w/ vendor-specific database (eg: mysql). Currently the only existing driver is for MySQL database. Returns rows as an associative array. The driver is responsible for sanitizing data ( eg mysql_real_escape_string() ). Has some similarities to PEAR DB, like selectOne() and selectColumn(). Example on how to use this class follows:

// include the relevant sources
// you'll have to alter this require statement 
// or set your include path
// DBWrapper.php and MySQLDriver.php files should be
// in the same directory
require_once 'DBWrapper.php';
// instantiate a MySQLDriver object
$driver = new MySQLDriver();
// make connection, here I'm using 
// defined constants loaded from a 
// config file, but you can hard 
// code the values here just as well
$driver->connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// you cannot use the driver, you make a DBWrapper by giving it the driver
$conn = new DBWrapper($driver);
// now you can use it
$rows = $conn->selectRows("select * from mytable");

BSD license.

ActiveRecord pattern implementation for PHP

This class if for PHP 4 or PHP 5

Eventhough active record is a great pattern, I find little use for it since every project involves table joins which active record is not designed to accomodate.

An ActiveRecord with minimum of fuss. Finder behavior is separated out into an ActiveRecordFinder class. ActiveRecord uses the last mysql db connection made, which is not very flexible. ActiveRecordFinder uses DBWrapper which sounds like a good idea, but for now means its not only dependent on DBWrapper, but also on the Registry class which will hold on to the only instance of DBWrapper in the application. Registry is just a hash to hold some objects in, but I do prefer it to having to say global \$hash.

guestbook software

Simple guestbook software that was written in one day and uses CSS for layout. Includes some anti-spam features and is easy to set up and use. The idea is you edit the CSS file to customize the guestbook to your site.

PHP versions 4 and 5. BSD license.

image copying class

When handling image uploads with PHP you need to be careful to prevent uploading of malicious files. In addition to normal best practices, this image copying class can be used to make copies of image files. The idea being, you might want to do some preliminary verification of an uploaded image file, then rather than simply copy() or move_uploaded_file() you might want to use php gD functions to create a copy of the image to the desired desitination.

PHP versions 4 and 5. BSD license.