Classes and funtions

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

Threaded View
Hi All

I'm pretty new to PHP.  I've done some relatively complicated bits now:
written my own little shopping cart, written a content management system
with file uploads, built in newsletters, etc, etc.

The problem is, my code is probably not very elegant.  For example, in my
CMS I have a page for adding pages and a page for editing pages.  Much of
the code is repeated in the two pages.

So, what I'm having difficulty with is what's the difference bewteen a class
and a function?  A function to me looks like a piece of code in an external
file (possibly) that I can call when I need it instead of duplicating code
in different files.  A class looks to achieve the same thing.  Am I missing
the point somewhere?



Re: Classes and funtions

Check this out:

AJ wrote:

Quoted text here. Click to load it

Re: Classes and funtions

Quoted text here. Click to load it

Yes, a class is designed to "encapsulate" data and code into one structure
so you as a programmer don't have to worry about how it works inside, but
how to use it from the outside.

For example

class foo {
   var $a;

    function foo($default) {
        $this->a = $default;

    function inc($amount) {
        $this->a = $this->a + $amount;

    function getValue() {
        return $this->a;

$oFoo = new foo(0);
echo $oFoo->getValue();

Or somebody else may design foo like this:-

class foo {
   var $a;

    function foo($default) {
        $this->a = $default;

    function inc($amount) {
        $this->a += $amount;

    function getValue() {
        return $this->a;

How the function inc works is not important, you just need to know how to
call it, and it will manipulate the data without you knowing how it does it,
or where it is storing the data.

With procedural code you'd probably write:-

function inc($value , $amount) {
    return $value + $amount;

$value = 0;
$vaule = inc($value , 5);
echo inc($value , 5);
echo inc($value , 5);

$value is only retained should you assign it to a variable. In the class
structure the is retained until the class is destroyed or manipulated again.

Your inital problem is not to learn how OOP works but to learn how to
rationalise code so that you are not writing the same code twice. Learning
OOP is a good idea(tm).

Re: Classes and funtions

Hi Andy,
a class is an abstraction that tries to model the real world, building
objects similar to the real ones.

For example you could think to a car like an object. The car has some
attributes: color, model, size, etc...

and some functions: you can open the door, you can drive, you can brake,

Now imagine that you are building a software and you need to represent a

An object oriented way to do this could be (in a pseudolanguage):

public class Car {

    private color;
    private model;

    public getColor() {
        return color;

    public setColor(color) {
        this.color = color;


that is nothing more than a piece of code, you might write the same
functions i a procedural language, in the same file o in more than one file.
The important thing is:

whit the object oriented programming you can create objects and after you
can create other objects which inherits their characteristics. In other
words, you can build things without reinvent the wheel each time.

I'm not a fanatic of the OOP, sometimes it works sometimes not, it depends
on your needs and on your project.

I know I've been a bit confused, but in a little space and not in my mother
tongue language is very difficult :)))


Quoted text here. Click to load it

Re: Classes and funtions

Quoted text here. Click to load it

Well you would really need to read some stuff on OO programming. But let's
say that (if applied properly), OO could give  you more reuse than you could
achieve with just functions. For instance, you could have CMSItemForm class,
which would handle all general form handling in your cms, and 2 subclasses
of it CMSNewsForm and CMSArticleForm, which would handle only parts specific
to news or article form, and everything else would be inherited from

-- /

Re: Classes and funtions

AJ wrote:

Quoted text here. Click to load it

You seem to be close with your definition of a function.

a function is a 'named' chunk of code.. it's called a function because
it *does something*.

(If you're unsure of the concept - think of it in real-world terms,
similar to the english dictionary definition of 'function'..  for
example, a function of your wristwatch is that it tells you the time...
another function of your wristwatch may be that it sounds an alarm.)

when you use a function by calling it's 'name' (known as "calling a
function") it executes the chunk of code.

as you guessed, a function makes this single chunk of code reusable
anywhere else in your code.

On the other hand, a Class is a completely different concept... In fact,
it's a horrible concept to try and understand what it is at first, and
even worse, try to understand *why* it's useful... But i'll try to
explain :-)

So.. first off, Forget programming for a second.. forget your project
and your code.. the best way (IMHO) to try and understand what a class
is, is to think about it in real-world terms.

Think of a "class" as a "blueprint". (for example, new houses on a
housing estate have a single blueprint that is used for all houses)..
for the english dictionary definition, it relates closely to

a "class" is a "blueprint" for an object... in the same way that a
blueprint may be for a new house on an estate...   Hopefully you
understand what an object is, else this may be a little confusing
already :-)

And see all those new houses on the housing estate..   They're all built
to the same specification (as per the blueprints)...  The specification
of each of these houses includes properties/attributes (such as Colour
of front door, garden plants, etc )..  and some Functions (such as "turn
on the tap", "open garage door", "open the window")... but of course
these functions can only be performed on the specific house (I mean, you
don't turn on your tap and run your neighbor's bath do you?)

When all these houses are first built, they're all pretty much
identical, however, they are seperate houses which exist on their own
independently of each other - the only thing that links them is their
original specification/blueprints...   Of course, when people move in,
they can be repainted, have different plants in the garden..  etc.

In the same light, all your objects which are constructed from your
class have the same properties/attributes (Data variables),  and also
contain a bunch of functions that can be performed on the object
(usually to modify the data variables of the object).

For example.  your class may be called RollOverHyperLink  ..  The
attributes being maybe IdlePicture, ActivePicture, height, width,
border, linkURL, alt-text etc.. you get the idea..   Functions could be
Set_To_Active, Set_To_Inactive.

Then you could have loads of buttons on your webpage that are Rollover
hyperlinks which are new objects based on the class RollOverHyperLink ..
each of them given unique properties which are appropriate to every
specific object (or 'instance')..  one may have IdlePicture set to
billgatescat.jpg and linkURL set to">, and another may
have IdlePicture set to Tonyblairinstockings.jpg with linkURL set to ... etc.. Hopefully you get the idea. :-)

Ben Cottrell AKA Bench

This post may contain explicit depictions of things which are "real".
These "real" things are commonly known as 'life'! So, if it sounds
sarcastic, don't take it seriously. If it sounds hazardous, Do not try
this at home or at all. And if it offends you, just don't read it.

Re: Classes and funtions

 .oO(Ben Cottrell)

Quoted text here. Click to load it

Hmm, how would you hyphenate this? ;)


Re: Classes and funtions

Michael Fesser wrote:
Quoted text here. Click to load it

Speaking of which, look at this *terrible* URL for a university:

(I'm not kidding! It's a university in california! How in their maddest moments
could they not look at that url and say 'err, it will read strangely to someone
who has never heard of nippissing!')

Mysteriously, they've changing the URL to - I wonder why? :)

also see:

(both of which are safe for work and not rude in any way)

etc. etc.

Re: Classes and funtions

Alex Hunsley contained the following:

Quoted text here. Click to load it


both equally safe for work/home/family etc.

Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs /

Site Timeline