By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,497 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,497 IT Pros & Developers. It's quick & easy.

Reducing code clutter

P: n/a
Hi all,

Newish PHP programmer here. I wrote a form select class to help reduce code
clutter, but I don't think the effort was worth it. I was hoping to post my
attempt and get some ideas from more advanced users on how to implement a
select form using less lines of code.

First the select form class which implements a select widget. I'll post only
the relevent parts:

class frmSelectClass
{
// Attributes
var $name = '';
var $size = '';
var $multiple = '';
var $id = '';
var $class = '';
var $style = '';
// Options
var $options = array();
function frmSelectClass($arg = '')
{
if ( gettype($arg) == 'array' )
{
foreach ( array_keys($arg) as $key )
{
if ( $key == 'name' )
$this->name = 'name="' . $arg['name'] . '" ';
elseif ( $key == 'size' )
$this->size = 'size="' . $arg['size'] . '" ';
elseif ( $key == 'multiple' )
$this->multiple = 'multiple="' . $arg['multiple'] . '" ';
elseif ( $key == 'id' )
$this->id = 'id="' . $arg['id'] . '" ';
elseif ( $key == 'class' )
$this->class = 'class="' . $arg['class'] . '" ';
elseif ( $key == 'style' )
$this->style = 'style="' . $arg['style'] . '" ';
}
}
}
function addOption($arg = '')
{
$value = '';
$label = '';
$selec = '';

if ( gettype($arg) == 'array' )
{
foreach ( array_keys($arg) as $key )
{
if ( $key == 'value' )
$value = 'value="' . $arg['value'] . '"';
elseif ( $key == 'label' )
$label = $arg['label'];
elseif ( $key == 'selec' && $arg['selec'] == $arg['value'] )
$selec = 'selected ';
}
}

$this->options[] = "<option $value $selec>$label</option>\n";
}
function output()
{
$attributes = $this->name . $this->size . $this->multiple .
$this->id . $this->class . $this->style;
$retval = "<select $attributes>";
foreach ( $this->options as $opt )
$retval .= $opt;

echo $retval . '</select>';
}

}

A tad more complicated than what I was hoping for. Now I'll demonstrate how to
use the class to create a select form on a webpage.

$frm = new frmSelectClass( array('id'=>'weekday', 'name'=>'frmDow') );
$dow = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );

for ( $i = 0; $i <= 6; $i++ )
$frm->addOption(
array( 'value'=>$dow[$i], 'label'=>$dow[$i], 'selec'=>date('D') )
);

$frm->output();

That's about it. I thought I was being really sneaky by passing in arrays
that hold attributes, so this:

$frm = new frmSelectClass( array('id'=>'weekday', 'name'=>'frmDow') );

would yield this:

<select id="weekday" name="frmDow" >

It works, but I think it would've taken less lines of code to just use the
straight forward non-OOP method:

$id = 'weekday';
$name = 'frmDow';
echo "<select id=\"$id\" name=\"frmDow\">

I'm sure lots of people have tried to abstract HTML forms using PHP classes.
It seems like a natural thing to do.

Any ideas on how to make the code more compact?

Thanks for reading down this far. ;-)

Pete
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Peter Salzman wrote:
Hi all,

I'm sure lots of people have tried to abstract HTML forms using PHP classes.
It seems like a natural thing to do.

Any ideas on how to make the code more compact?

Thanks for reading down this far. ;-)

Pete

There are more implemtation of form element abstrations around, but I
havn't used any so I don't know exactly where they are.

However, abstration isn't always about less code, sometimes it's about
convience. For instance, in your class you eliminated the HTML from the
main script, and it's created by the output function. Thus, your main
code is less cluttered with HTML, and more direct processing.

I tried implementing the HTMLDom once using php classes, and it works
ok, but I think the effort was probably in vein as creating even the
simplest of things becomes a really large pain using the DOM structure.
I started making a few abstract methods for common items (like a
select box) but eventually decided it wasn't worth in.

Something like what you have now is useable though. It's pretty simple
and seems like it's fairly easy to use. The # of lines in the end may
be the same, but the code probably looks better.
Jul 17 '05 #2

P: n/a
*** Peter Salzman wrote/escribió (Fri, 24 Jun 2005 03:17:50 +0000 (UTC)):
if ( $key == 'name' )
$this->name = 'name="' . $arg['name'] . '" ';
elseif ( $key == 'size' )
$this->size = 'size="' . $arg['size'] . '" ';
elseif ( $key == 'multiple' )
$this->multiple = 'multiple="' . $arg['multiple'] . '" ';
elseif ( $key == 'id' )
$this->id = 'id="' . $arg['id'] . '" ';
elseif ( $key == 'class' )
$this->class = 'class="' . $arg['class'] . '" ';
elseif ( $key == 'style' )
$this->style = 'style="' . $arg['style'] . '" ';

A little suggestion: http://www.php.net/switch

--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Jul 17 '05 #3

P: n/a
Alvaro G Vicario wrote:
*** Peter Salzman wrote/escribió (Fri, 24 Jun 2005 03:17:50 +0000 (UTC)):
if ( $key == 'name' )
$this->name = 'name="' . $arg['name'] . '" ';
elseif ( $key == 'size' )
$this->size = 'size="' . $arg['size'] . '" ';
elseif ( $key == 'multiple' )
$this->multiple = 'multiple="' . $arg['multiple'] . '" ';
elseif ( $key == 'id' )
$this->id = 'id="' . $arg['id'] . '" ';
elseif ( $key == 'class' )
$this->class = 'class="' . $arg['class'] . '" ';
elseif ( $key == 'style' )
$this->style = 'style="' . $arg['style'] . '" ';

A little suggestion: http://www.php.net/switch


Given the structure, *any* conditional branching is redundant:

foreach ($arg as $key=>$val) {
print " $key = '$val' ";
}
C.
Jul 17 '05 #4

P: n/a
Peter Salzman wrote:
Hi all,

Newish PHP programmer here. I wrote a form select class to help reduce
code
clutter, but I don't think the effort was worth it. I was hoping to post
my attempt and get some ideas from more advanced users on how to implement
a select form using less lines of code.

First the select form class which implements a select widget. I'll post
only the relevent parts:


Pete, IMHO the value of abstraction of this sort in a short-transaction
environment like the web is severely overrated, to the point of being an
obstruction to productive work. Did you by any chance come from a desktop
dev environment?

My own work is all around databases. If yours is also, you may benefit from
what I did. It centers around having a data dictionary specifying what the
columns are in all of the tables (it is much more than that, but that is a
good introduction). When it comes time to generate HTML widgets, you read
the dd and look at what kind of columns you have and generate appropriate
widgets (mostly textbox, textarea, and SELECT lists).

If you can give more context about the kind of site you are working on
perhaps myself or others can give more details.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #5

P: n/a
Kenneth Downs <kn**************@see.sigblock> wrote:
Peter Salzman wrote:
Hi all,

Newish PHP programmer here. I wrote a form select class to help reduce
code
clutter, but I don't think the effort was worth it. I was hoping to post
my attempt and get some ideas from more advanced users on how to implement
a select form using less lines of code.

First the select form class which implements a select widget. I'll post
only the relevent parts:


Pete, IMHO the value of abstraction of this sort in a short-transaction
environment like the web is severely overrated, to the point of being an
obstruction to productive work. Did you by any chance come from a desktop
dev environment?

My own work is all around databases. If yours is also, you may benefit from
what I did. It centers around having a data dictionary specifying what the
columns are in all of the tables (it is much more than that, but that is a
good introduction). When it comes time to generate HTML widgets, you read
the dd and look at what kind of columns you have and generate appropriate
widgets (mostly textbox, textarea, and SELECT lists).

If you can give more context about the kind of site you are working on
perhaps myself or others can give more details.


Hi Ken,

I'm not a programmer by trade. It's just something I really enjoy doing.
I'd rather program than anything else in the world. I'm actually a physicist
by trade (well, a grad student by trade, but will soon be a physicist by
trade).

I'm writing my own blog, so I fit under your "short-transaction" model. I
know there are more blogs out there than you can shake a fist at (some quite
good), but I wanted an excuse to learn sessions, SQL and using
mysql/sqlite/pgsql securely with PHP.

The blog itself has been a wonderful learning experience (I've only written 3
posts, although I've spent an awful long time writing the blog itself!). My
sole interest, really, is to become a better programmer and to have fun doing
it.

BTW, I really appreciate the other answers to my post. You guys had some
really good stuff to say!

Pete
Jul 17 '05 #6

P: n/a
Peter Salzman wrote:
Kenneth Downs <kn**************@see.sigblock> wrote:
Peter Salzman wrote:
> Hi all,
>
> Newish PHP programmer here. I wrote a form select class to help reduce
> code
> clutter, but I don't think the effort was worth it. I was hoping to
> post my attempt and get some ideas from more advanced users on how to
> implement a select form using less lines of code.
>
> First the select form class which implements a select widget. I'll
> post only the relevent parts:
>
>
Pete, IMHO the value of abstraction of this sort in a short-transaction
environment like the web is severely overrated, to the point of being an
obstruction to productive work. Did you by any chance come from a
desktop dev environment?

My own work is all around databases. If yours is also, you may benefit
from
what I did. It centers around having a data dictionary specifying what
the columns are in all of the tables (it is much more than that, but that
is a
good introduction). When it comes time to generate HTML widgets, you
read the dd and look at what kind of columns you have and generate
appropriate widgets (mostly textbox, textarea, and SELECT lists).

If you can give more context about the kind of site you are working on
perhaps myself or others can give more details.


Hi Ken,

I'm not a programmer by trade. It's just something I really enjoy doing.
I'd rather program than anything else in the world. I'm actually a
physicist by trade (well, a grad student by trade, but will soon be a
physicist by trade).


Can appreciate that double life. When I was supposed to be doing Maxwell's
Equations I was usually off hacking somewhere. Hence I am programmer by
trade with a BS Physics.

I'm writing my own blog, so I fit under your "short-transaction" model. I
know there are more blogs out there than you can shake a fist at (some
quite good), but I wanted an excuse to learn sessions, SQL and using
mysql/sqlite/pgsql securely with PHP.
This is a perfect way to learn those things. The existence of those blogs
gives you a standard against which to measure your efforts.

The blog itself has been a wonderful learning experience (I've only
written 3
posts, although I've spent an awful long time writing the blog itself!).
My sole interest, really, is to become a better programmer and to have fun
doing it.


My suggestion is Ken's Umpteenth Law: Don't Abstract Against Non-existent
Cases. You are introducing a level of abstraction far above what is
required for a blog. Further, you are doing so without the intuitive feel
that comes with more years of experience, so you are risking entry into the
hall of mirrors, where you will be lost, forever lost. Therefore, commit
to writing a decidely NON-ABSTRACT blog that does what it is supposed to
do. Then gradually refactor the code to create general-purpose routines
wherever you see repetition.

Hope this helps.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 17 '05 #7

P: n/a
I suggest you take a look at MVC Design patterns (just google like I wrote
;) ), and also take a look at refactoring code techniques (which is fancy
way to say - reducing code clutter in old code base ;) )

--
Dominik Susmel | art director
www.vongestern.com | vonGestern art company . Zagreb, Croatia
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.