Functions & Classes

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have been creating new projects and updating older projects to take
advantage of Classes.
I am curious on the school of thought concerning 'content - vs- display'.

I have been using 'functions' for all dynamic HTML displays and
'Classes' for processing content. I have read this somewhere, that
classes 'should' be used for processing not displaying.

Obviously this is not a functional barrier since you can accomplish this
just as easy with a Classes.

I would just like to hear your ideas on this or if this is an antiquated


Re: Functions & Classes

.oO(Scott Johnson)

Quoted text here. Click to load it

I use classes for everything, be it backend stuff or visual components.
The only non-class thing in my library are some little helper functions.


Re: Functions & Classes

Scott Johnson wrote:
Quoted text here. Click to load it


Maybe you're thinking about this incorrectly.  A class is a the
description of an object, like "automobile" is the description of a type
of motor vehicle.

When you instantiate a class, you create an object - just like when you
instantiate an object of the automobile class you get a specific vehicle.

Objects in both realms have state and behavior.  For instance, the car
has state - it's a red 2009 Corvette (3 states - color, hear and model).
  A programming object would have similar states.

Additionally, the automobile has behavior - accelerate, brake, turn,
etc.  If you are currently going 30 mph and want to speed up to 60 mph,
you don't set the "speed" state in a car.  You execute the method
"accelerate" until you reach the desired speed.

The same way, a programming object would have behavior.  You don't
generally change the state directly - you do it through the appropriate

As a real-life programming example, take an item in a store.  The object
has state - stock number, description and cost.  It also has behavior -
when you get more of that item in, you have a method addStock() where
you add the number of items which came in.  You don't set the new count
directly, the object does that for you.  And when you sell an item, you
have a sellStock() method which subtracts the number of items sold (and
maybe throws an exception if you don't have that many in stock).

When you build your classes, you should keep this in mind.  "Processing
content" isn't really a good use of classes - unless you can relate that
content to a "real" (in programming terms) object.

I do use classes for both display and processing.  For instance, I have
a class which represents shipping information (name, address, etc.).  It
has a showForm() method, which generates the HTML to display the
shipping form.  It also has a parseForm() method which takes the $_POST
(usually) array as a parameter and parses the information from the form
and validates it.  If there are errors, it returns false from the
parseForm() method (I need to eventually make this throw an exception -
it's been around for a while).  If there is an error, the script can
call the showForm() method again to display the form again, this time
with the fields filled in and an asterisk by each incorrect field and
error messages at the top.

There are other methods also (i.e. to add the information to a
database), but this is the main part.

I hope this (rather long) description helps.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Functions & Classes

Jerry Stuckle wrote:
Quoted text here. Click to load it


Thanks to both you and Micha for your ideas.

I was hoping this was common practice since it was the way I wanted to go.

Now off to consolidate.


Site Timeline