[thelist] php design question

R.Livsey R.Livsey at cache-22.co.uk
Mon Nov 18 21:08:01 CST 2002


I'm going to try a take on this :)

its now 3:00am and Ive been working all day I appologise if this makes
no sense!

Part of the confusion here is coming from comparing includes to classes
and OO code.
Includes may contain classes, but that's pretty much where the
comparison ends.

I use classes entensively over at www.cache-22.co.uk, view the php
source of any page (the <?php?> link on every page) to see the code, it
may help or it may not :)

For an example I will talk about the calendar on my site.
I have a calendar class
(www.cache-22.co.uk/source/classes/class.calendar.php) which contains
all the code for the calendar object.
Then I have a page (www.cache-22.co.uk/source/blog/) which creates and
instance of the calendar:

$cal = new Calendar($month, $year, $day, $dates);

$dates is another object which contains stuff to check dates for
validity etc, but we don't need to worry about that here.

So now we have a calendar object I can access all its methods (functions
basically) and varibles through.
For example on my blog page I set links for certain days by doing the
following:

$cal->AddLinkToDay($day, $link);

And then I can call the GetHTML() method of my calendar object and it
will do some stuff to generate the HTML. GetHTML() happens to go through
all the days in the month, add a link if necessary, set the class of the
td depending on certain things and then return the html. But we don't
need to know how it works - it just does it. It hides the implementation
from the user so you don't need to know what is going on.

I could have implemented all this the same by having a bunch of
functions doing exactly the same thing or including files with bits of
code in them that do what you want but the point is that it makes the
code easier to write, read, debug etc..

Another of the main points is for code reuse. Someone else should be
able to take any of the classes and reuse them without so much as
changing a line of code. All you need to know is how to implement it and
you are away.
It's a black box in that sense, many OO books give reference to things
like tape recorders. You don't need to know how it works underneath the
case, you don't need to know any theory about magnetism and electronics,
you just need to know that if you press things in a certain order then
it records and if you do things in a different way it will playback.
An OO version of a tape recoder could be something like:

$vcr = new TapeRecorder();
$vcr->loadTape('tape name');
$vcr->setChannel(2);
$vcr->record();

// an hour later we want to watch our program..
$vcr->stop();
$vcr->rewind();
$vcr->play();

Its abstracted what the vcr is doing internally and is much easier to
read than a textbook on electronics.
Back to the calendar example - someone can take the class, read the
documentation on how to use it
(http://www.cache-22.co.uk/articles/Programs/R.Livsey/C22_Calendar_Class
) and they can then use it without ever looking at how the class works.

Anyway, in summary it's a different way to program and nothing to do
with includes :)

Probably muddied the water even more but it was worth a try!

hth

--
R.Livsey
Incutio Web Developer
[ PHP | Perl | Java | C# ]
w : www.cache-22.co.uk
  / www.incutio.com





More information about the thelist mailing list