Following on from windandwaves's message. . .
Hi Folk
I was wondering to hear from you why you use classes in some instances
rather than functions. I have never used a class, but I use a lot of
functions.
I don't have a comp sci background, so I may miss some of the basics.
TIA
- Nicolaas
I know where you're coming from.
Look on classes and OO as a good way to do lots of things. You'll still
use 'stand alone' functions but _thinking about_ things in an OO way
should make a big difference. Just like your function library took time
to develop so will your collection of handy objects.
For example I have database and table classes to replace my collection
of database related functions. A lot of the code is the same, but one of
the things I can do now is prepare and cache certain things to assist
performance.
There are plenty of easy examples. See chapter 14 of the PHP manual. An
excellent example is creating an email. There are a number of classes
around which basically let you set a load of elements then do the dirty
work for you.
One example is having a user class (and working with instance objects).
[You can think of _class_ as being the ideas that make it work, the
design and interrelationships with other objects, and the _object_ as
the practical realisation which gets processed.] My standard user class
handles creating a new user, interfacing with a database table, managing
passwords, managing permissions and change of email address etc. At the
top of every page I can do /something/ like
session_start();
$cu = GetCurrentUser(); // this is a stand alone function
$cu.CheckPermissions('IsAdmin'); // this is an object-function
The thing is I know that all my user related data and procedures are in
the one place and the messy bits are nicely wrapped up. If experience
shows that the user class could usefully be enriched then I only have to
visit one bit of code to do it knowing that the behaviour will not be
adversely affected by no-longer applicable functions. For example there
is a class-function to validate a password then I thought it would be
great to allow another developer to plug-in their own function. Not a
single bit of page code had to be changed to make use of this feature,
it was all handled 'inside' the class.
There are other good reasons for adding the ability to write and use
classes to your repertoire but it is too large a subject to go into
here.
--
PETER FOX Not the same since the bolt company screwed up
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>