Page 1 of 1
Classes vs. bunches of functions
Posted: Mon Jun 07, 2004 8:17 pm
by dull1554
My question is this: what are the advantages to using a class as opposed to using say just one include file that has all the functions that you need?
whats the advantage to something like this
Code: Select all
class blah
{
var $string = "blah";
function say()
{
echo $this->string;
}
}
over something like this
Code: Select all
$string = "blah";
function say()
{
echo $string;
}
they both produce the same thing i honestly dont see any advantages to using one over the other, im working on a imap mail application, i was thinging about building a class for that, but a group of functions seems simpler
any thoughts?
Posted: Mon Jun 07, 2004 8:49 pm
by jason
Well, hehe, for starters, the second one doesn't work.
Seriously though, it's not that Classes are better than functions. It's a way of development (OOP, or Object Oriented Programming). Building a class just to build a class will obviously offer garner you no benefits; however, programming in an OO way will.
Posted: Mon Jun 07, 2004 8:59 pm
by dull1554
honestly in your mind what are the benifits to programming in OOP in PHP, i find it just as easy and most of the time less confusing not to program with object orentation
Posted: Mon Jun 07, 2004 9:07 pm
by markl999
Why use OOP?
read this

Posted: Mon Jun 07, 2004 10:01 pm
by dull1554
after reading that i am intrigued and a little confused, ill give it a little time to sink in then ill comment on it.....
Posted: Mon Jun 07, 2004 11:17 pm
by kettle_drum
Most of the reasons are because of re-useability. By making classes you just make sure that all the methods (functions) and attributes (variables) are together and can run in any situation without messing up any other bits of code. A class is a self-containted unit that you can just include and initialise. Yes you can just include a file full of functions and variables - but what then happens if your program uses the same variable names? "well i would make sure it didnt" i hear you say, but what happens when you stop working on the code and somebody else starts to look after it? They might then port it to several other projects and things start to get hard as they cant use any of the understandable names for functions.
Classes just make it easier to re-use the code as you can send somebody a class and tell them to include it and initialise it, and this code has been well tested by yourself and it works and its held together nicely in a class.
Until you start to code in OO you wont like it, but once you have tried it and got a feel of it then you will love it and will always code in it. Yes its pointless to make a class for your example, but make a class to deal database interaction, that you tell what kind of database you want to use, and it then connects to it and uses the correct commands to query that type of database and you have an encredibly good class that you can use on all of your projects.
Posted: Tue Jun 08, 2004 6:04 am
by dull1554
i get everyones point now, after reading that article last night i can see the uses of a class, and some day i may start using them
Posted: Tue Jun 08, 2004 6:44 am
by magicrobotmonkey
I find that when writing an app for a business (which I do a lot) OOP makes it easier to "replace" whatever task they want me to. I meet with them and they explain what it is that they do that they want the app to do in the future. Once I have a satisfactory idea of the procedure, it is generally pretty easy to move that into an OOP design. It seems to me like OOP makes it easier to relate a program to its real-world equivalent.
Posted: Tue Jun 08, 2004 6:45 am
by launchcode
Even for the most simple of projects an OO approach (when followed correctly) will allow for far better code structure and design than a bunch of functions could ever hope to do.
Posted: Tue Jun 08, 2004 10:23 am
by patrikG
Also, Zend has an open-minded article on it:
http://www.zend.com/zend/art/oo-proc.php