I while back I started work on a PHP framework, pretty much the same as Ruby on Rails' Active Record, but on PHP and coded by yourself truly. I named it YaPHPfter: Yet Another PHP Framework.
I need to organise my thoughts around it, so I'll be writing about it now and then, when I get the time, on the site. I tried to work on it again last night, but only sat there, looking dazed and confused, because the last time I worked on it was late at night, and I had no idea of what I did previously and what I should do further.
This is already the second incarnation of YaPHPfter, the first one is alive and kicking, and, if you're interested, I can supply the code, but at this stage it's quite imbedded into the test app I developed around it, so it will require heavy modifications to be useable.
Anyway, enough of the past, on to the future!
The idea is to have PHP code which will automate stuff like DB calls and HTML form generation. Both which is very structured and contains a lot of patterns. The art of coding lies in recognizing patterns and exploiting them in loops and control structures. YaPHPfter tries exactly that with DB calls and HTML forms, which are obviously related. I also try to apply the principles of Convention over Configuration and Don't Repeat Yourself.
An example. Say we want to capture data about our clients. We will have the following fields in our clients table:
We will also have a contacts table which will store the contact information for clients:
Conventionally you will create to HTML forms to capture this information, including input tags:
<input type="text" name="name" id="name" size="60" />
Each field will get an input tag, with at least the name attribute set to the field name. We have our first pattern! By looping through the field names, we can dynamically create the input tags for a simple HTML form:
$fields = array('name', 'surname');
$toprint = '';
foreach($fields as $field_name) {
$toprint .= '<input type="text" name="'.$field_name.'" id="'.$field_name.'" />';
}
This ensures that we have consistency in the naming and the format of our input elements. More input tags can also be added by just adding them to the $fields array.
This piece of code can be extended to include more attributes for the input tags:
1 $fields = array(
2 'name' => array('size' => 50, 'value' => $name),
3 'surname' => array('size' => 50, 'value' => $name)
4 );
5 $toprint = '';
6 foreach($fields as $attrib => $field_array) {
7 $field_array['name'] = isset($field_array['name']) ? $field_array['name'] : $attrib;
8 $field_array['id'] = isset($field_array['id']) ? $field_array['id'] : $field_array['name'];
9 $field_array['type'] = isset($field_array['type']) ? $field_array['type'] : 'text';
10 $toprint .= '<input';
11 foreach($field_array as $attrib => $a_value) {
12 $toprint .= ' '.$attrib.'="'.$a_value.'"';
13 }
14 $toprint .= ' />';
15 }
In lines 1 - 5 we define the fields, giving them some extra attributes. The value attributes are given values contained in variables. More about that later... In lines 7 - 9 we check if the necessary tags are all there, and assign default values if they aren't. The id attribute gets the same value as the name if no other value is specified. In lines 11 - 13 we add all of the attributes to the input tag.
Schweet! We can now easily and quickly generate input tags for an HTML form. This should at least get you thinking. What I'll be writing about next is to extend the code to include formatting and placement for the input elements, and how to safely get content of the $name and $surname variables on lines 2 and 3.