.:] patterns.for.php.001-factory [:.

August 9th, 2007 – 13:01:33

One of the marks of a good language is the ability to apply the principles of Design Patterns to a project written in that language. PHP has come a loooong way in this sense in the jump from PHP3 -> 4 -> 5.

This will be part 1 in a series of unknown length where I will publish simple to use pattern implementations for PHP with examples and downloadable source code. *hooray Blog software update*

Lets start with a basic factory object. Examine the code below:

// First we define our interface for the object we will be working with
interface Auto {
public function go();
}

// Now let's define our implementation classes of Auto
class Ford extends Object implements Auto {
public function __construct() { parent::__construct(); }
public function go() { print("*sputter* *sputter* *cough*"); }
}

class Chevy extends Object implements Auto {
public function __construct() { parent::__construct(); }
public function go() { print("*vrooooooOOoOOOoOoOOom"); }
}

// And finally our factory
final class AutoFactory {
public static function getFord() { return new Ford(); }
public static function getChevy() { return new Chevy(); }
}

// Now lets see what happens
class Main {
public $car;
public function __construct() {
$this->car = AutoFactory::getFord();
$this->car->go();
$this->car = AutoFactory::getChevy();
$this->car->go();
}
}

$running = new Main();

The output from this small program would be as follows:

*sputter* *sputter* *cough*
*vrooooooOOoOOOoOoOOom

There you have it, a simple PHP implementation of the Factory pattern — in my opinion, one of the most widely used and important of all Software Design Patterns, especially when practicing dependancy management for you application.

Anyhow, tune in tommorow to find out how to apply the DAO pattern to PHP5 thereby abstracting your Persistance Layer even further from your application and allowing you to change your Database Abstraction libraries (PHP, AdoDB, Pear, etc) and leave your actual data manipulation code alone, as well as your business logic.

Source Code for patterns.for.php.001-factory

.:] $this->classLoad($php) [:.

August 8th, 2007 – 13:42:40

One of the things I always forget to do when working on something in PHP is “require” or “include” my class definition files, it`s kind of like remembering to import classes in Java — which I always forget to do, thus the reason I use an IDE with the auto-import feature! However, no PHP IDE I have seen has this ability, but, PHP does provide you with a method to write your own auto-import functionality write into your application. Say for example you have the following code:
(more…)

.:] $this->php(beans) [:.

August 8th, 2007 – 13:40:20

When I am working in Java, I code in IntelliJ which has this nice little macro to create all my getters and setters for the Bean object that I am working on at that point in time. It is a huge time saver. I recently started a new project in PHP and found myself desiring the same functionality in my PHP IDE, which didn`t exist. After a little bit of research, I decided I could do one of two things, either hack together a little macro to do it for me, or just create some function that would do it dynamically, on the fly… Here is what I came up with, it is likely that this will end up as part of a bigger PHP Framework that I will work on when I have some spare time (notice how long it has been between blog entries to get a clue how much spare time I have had lately) that will be some port of the JavaBeans API for PHP… Anyhow, I hope you enjoy!
(more…)

.:] irrelavent.relevency [:.

August 8th, 2007 – 13:37:52
Tagged as: java, programming

So it seems like everyone has there little voodoo algorithms on how to calculate how relevant a search result is to the term that was being searched, and I personally, do not think it should be thus. So I am going to share my rough draft for a simple relevancy calculation between a String, hereafter reffered to as query, and a search result, hereafter referred to as SearchResultVO.

(more…)

.:] Java.Revver.XmlRpc.SSL [:.

August 8th, 2007 – 13:35:30
Tagged as: api, java, revver, xmlrpc

I have started a new project on SourceForge called RevverJ, however, it is pending approval still (just created today), so in the meantime, I thought I would share how I finally (after about a week of fighting) was able to communicate with the Revver API from Java.

(more…)

.:] xmlhttprequest.and.you [:.

August 8th, 2007 – 13:33:34

So this week I had an enhancement at work which required me to write some new Ajax functionality for the website. The enhancement was rather trivial really, essentially I needed to drop something on the session real-time so that I could get at it later.

So I wrote myself a quick `n` dirty little servlet to handle the back end, and a JavaScript function which acted as a client-side version of HttpSession.setAttribute().

(more…)

.:] mycrack.tips.and.tricks [:.

August 8th, 2007 – 13:30:56
Tagged as: css, design, myspace

Like a billion other people on the web, I spend some time on MySpace.Com for various reasons. I maintain a blog over there as well where I post lyrics for songs and such stuff that I have written. I have a couple of band profiles for a few of my projects, and blah, blah, blah.

Anyhow, I thought I would share 5 quick and easy customizations you can make to your myspace profile to make it a little more unique, without having to use some self-proclaimed myspace guru`s ad laden codes.

(more…)

.:] i.suggest [:.

August 8th, 2007 – 13:01:57

Somewhat recently an enhancement came the wire at work for text input fields that auto-completed based off of a static set of data, specifically, e-mail domain names. The enhancement came to me and it was a fun thing to create, and something that could be potentially very useful for alot of different pieces of the application, so I thought that I would share the pattern that we came up with to solve this problem.

(more…)