PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
$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
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.
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
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.
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.
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.