Is object orientated PHP scripting worth the effort?

I’m currently creating a movie review website in PHP an SQLite. I’m taking a very object orientated approach.

For example, all database access goes through an object. Adding a review is as simple as calling a function with the proper data.

Here is the function header:

addReview($assocArrayReviewData, $assocArrayReviewBlockData)

Each parameter is an associative array that corresponds to fields in the database. While I don’t want to get into too much detail, the second variable is actually an array of arrays, where each one holds a block of review information (eg: “What the box says”, “funny quotes”, “My thoughts”).

The best thing about is is that once I create the administration interface I can just call that one function with the correct parameters and bam, a new review.

The class also has a built-in search function (that could be used on the internal or external site).

My next class that I created for primarily for the administration area. It’s an html tag class object. You might me thinking, what’s the point of mimicking html tags? Well in my never ending quest to separate html from code, this object can be instantiated instead of writing html dispersed through the script code. Please forgive me on the naming. I’m not quite sure what to call the class yet. I first called a “form” class, then a “control” class, and then once I realized it could be used for anything… the “htmlTag” class. Well anyways, it does what I want it to do regardless of what it is called.

Here is a quick example of code that would call it:

$test = new HtmlTag('test', 'form');
$test->setAttribute('method', 'post');
$test->createSubControl('button1', 'input');
$test->subControl('button1')->setAttribute('type', 'button');
$test->subControl('button1')->setAttribute('value', 'Click Here!');
echo $test->toHtml();

Any idea what it does?

It creates this:

Pretty cool, eh? The whole class is a beefy ~220 lines.
Now I can create forms programmatically that can have an unlimited number of parent/children/sub-children to them. So a form can hold a select box, that holds option tags… etc.

Here is a bit of the class file:

class HtmlTag
  //stores the type of control (this should correspond to an actual html tag)
  private $controlType; 

  //hold any text or whatever inside the control's start and end tags
  private $innerHtml; 

  //associative arrays:
  //hold all of the attributes of the control (eg: onsubmit='' onclick='' style=''...)
  private $attributes; 

  //holds sub controls (eg: form has controls inside it, select control has options inside it)
  private $subControls; 


 function &subControl($name)
     return $this->subControls[$name];

The subControl function is key to accessing children and sub-children of the main object. I wanted to keep the array subControls private, so that function is necessary.

Now that I have that class created I will start designing the administration interface, that now, is just a shell that accesses these two classes.

This entry was posted in General and tagged , , , , , . Bookmark the permalink.