I was on a forum site when I noticed this question, began answering and thought it would be a great blog post.
“I am an experienced PHP programmer, but in all the situations I have been in dealing with data bases and other data manipulation I have never needed classes. I understand what they are and how to employ them, but can someone give me examples of when they would become really really important?” – Anonymous
1. Let’s say for instance, that you found writing out all these SQL statements became tedious and a lot of work. Why not write out a class that could create your SQL code for you? Well, this is what many Object Relational Mapping frameworks already do. Zend, CakePHP, Lithium, CodeIgniter. No more SQL (for the most part), simply Zend->save($data);
2. Let’s say you’d like to grab information from Facebook Graph API and you’d like to use that same code in 10 different websites. Writing a class would allow you to wrap up your code in a distinguished and organized structure, that a) you can share with other people b) your functions are likely not going to conflict with other peoples pre-existing functions.
3. Let’s say you would like to have the ability to do an echo $anyVariableOrObject. With PHP’s toString() methods, which are inherited to every class, any object can utilize the a function that was only written in one place.
Classes allow objects to be more organized and defined in highly complex environments. Object Oriented PHP is a regular tactic and can be found all over the place. Actually, PHP 5.3 is becoming even more Object Oriented as they have now added many additional features.
The basic idea of classes and object oriented programming grow into very helpful techniques:
- MVC – Model View Controller (Separation of Data, Actions, and Interface)
- ORM – Object Relational Mapping (Direct Database linkage)
- DRY – Don’t Repeat Yourself (Re-use code whenever possible)
I hope this helps! I’ll have to steal this conversation for a blog post as well. TY for the idea.