Page 1 of 1

Classes over includes?

Posted: Wed Mar 31, 2004 7:27 am
by Zay
hi,

I've been working for about a year with PHP now... but never really saw any use to work with classes... I saw some posts here saying classes are way better then includes...

could some of you explain me what makes classes better then includes, when to use them, and perhaps some small examples?

I know the basics of classes, I learned that from making countless number of java apps at college *yawns*.

or perhaps does anybody have a link to a nice sumerary?

many thanks!

Posted: Wed Mar 31, 2004 7:46 am
by kettle_drum
-Easier to re-use. Code one class that deals with database interaction and you can then use that on all your projects as the class contains all functions and variables needed.

-Since you re-use the class there is likely to be less errors.

-You can easily come back and recode a class in a few months to make it faster/expand it without having to change external code.

-You can make duplicate entries - imagine that the functions need a set of vars, in a class they are all there and are created when you call the class - with just functions you need to make more variables to hold everything each time.

-Looks nice and neat in the source, so its easier to see whats happening:

$myClass->run_this();

which may start the process of what the class does, and call each and every method in the class, whereas with just plain functions - to make it reusable and robust you need to do:

function1();
function2();
function3();
function4();

-Your data is protected in a class so theres less chance that you will accidentally change a variables data causing errors.

-Its a good way to organise things - put all related functions into one class so you know where everything is, stuff to do with the database put in a class call DB, and then you can instantly tell what everything does:

DB::insert()

can be easily identified as something to insert data into the db, but

insert();

is less easily identified - is it for a file, a socket, to include a template? t just helps to clarify things.

But overall i think its just down to your personal preference, before i started doing everything in classes i didnt see the point in them, but now i just do it and feel its the best way and wouldnt do it any other way.

Posted: Wed Mar 31, 2004 7:52 am
by Zay
ah, I see.

might be a nice thing to sort the functions in yeah...

but that would still be nothing more then with includes...

but on the other hand I presume it has a constructor & destructor?

things like that would make it better...

I'll go run some tests on it tonight :)
do you have an URL on some class refference (besides http://www.php.net :P)

Posted: Wed Mar 31, 2004 8:00 am
by patrikG

Posted: Wed Mar 31, 2004 8:01 am
by Pozor
hello,

i'm not completely sure but php classes haven't a destructor in PHP (a constructor yes for sure).
I thought they would be destroyed when the script is finished.

Please tell me if im wrong..

greez Pozor

Posted: Wed Mar 31, 2004 8:04 am
by patrikG
In PHP4 classes do not have a destructor. From PHP5 on they do :)

Posted: Wed Mar 31, 2004 10:16 am
by Zay
I see...

destructors can be really usefull for a lot of things though.
ofcourse it's not like delphi where you can use to free variables... but it can come in handy for log purposes for instance...

Posted: Wed Mar 31, 2004 2:58 pm
by Zay
well I've been messing with classes this evening... I think I have found a use for it now...

I built a nice database class which I will be implenting on my site tomorrow. it includes a function that generates queries (ofcourse :P).

thnx for the link patrikG

Posted: Thu Apr 01, 2004 1:57 am
by patrikG
no worries, mate :)