473,473 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Classes and funtions

AJ
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?

Cheers

Andy
Jul 17 '05 #1
8 2024

Check this out:

http://www.osnews.com/story.php?news_id=6788

AJ wrote:
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?

Cheers

Andy


Jul 17 '05 #2
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,
etc...

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

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 :)))

Bye
Fabrizio

"AJ" <no****@redcatmedia.net> ha scritto nel messaggio
news:ce**********@sparta.btinternet.com...
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?

Cheers

Andy

Jul 17 '05 #3
"AJ" <no****@redcatmedia.net> wrote in message
news:ce**********@sparta.btinternet.com...
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?


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);
$oFoo->inc(10);
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).
Jul 17 '05 #4
"AJ" <no****@redcatmedia.net> wrote in message
news:ce**********@sparta.btinternet.com...
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?


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
CMSItemForm.

rush
--
http://www.templatetamer.com/
Jul 17 '05 #5
AJ wrote:
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?


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
'classification'

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 www.microsoft.com, and another may
have IdlePicture set to Tonyblairinstockings.jpg with linkURL set to
www.number-10.gov.uk ... etc.. Hopefully you get the idea. :-)

--
Ben Cottrell AKA Bench

Disclaimer:
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.
Jul 17 '05 #6
.oO(Ben Cottrell)
[...]billgatescat.jpg [...]


Hmm, how would you hyphenate this? ;)

SCNR
Micha
Jul 17 '05 #7
Michael Fesser wrote:
.oO(Ben Cottrell)

[...]billgatescat.jpg [...]

Hmm, how would you hyphenate this? ;)

SCNR
Micha


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

http://www.unipissing.ca

(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 www.nipissingu.ca - I wonder why? :)

also see:

www.whorepresents.com
www.powergenitalia.com

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

etc. etc.

Jul 17 '05 #8
I noticed that Message-ID: <10*************@corp.supernews.com> from
Alex Hunsley contained the following:
also see:

www.whorepresents.com
www.powergenitalia.com


also www.cumstore.co.uk
and www.antiquesexchange.com

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 http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Cheetah | last post by:
Does anyone know of some public/open source implementations of Trig funtions - ie sin, cos, tan - for Java that operate with BigIntegers or BigDecimals. Even better would be a library which can add...
14
by: Pratts | last post by:
I am a new one who have joined u plz try to help me bcoz i could not find ny sutiable answer foer this Question Qus>>why do we need classes when structures provide similar functionality??
12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
6
by: Peter B | last post by:
I have MS Access 2000. I have recently installed it on a new computer and transferred my mbd files across. These work OK, including those using SQL Aggregate funtions. However if I set up a...
9
by: fgh.vbn.rty | last post by:
Say I have a base class B and four derived classes d1, d2, d3, d4. I have three functions fx, fy, fz such that: fx should only be called by d1, d2 fy should only be called by d2, d3 fz should...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.