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!
Classes over includes?
Moderator: General Moderators
-
kettle_drum
- DevNet Resident
- Posts: 1150
- Joined: Sun Jul 20, 2003 9:25 pm
- Location: West Yorkshire, England
-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.
-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.
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
)
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