Why use PHP Classes in Web Apps?

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

Absolutely Can!

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.

About Phillihp Harmon

I'm Phillihp. My name can be spelled the same way forwards and backwards, so can my posts... if you wish. I'm out here exploring, learning, and sharing what I find. This is more for fun and personal growth, I aim to be as consistent as possible, so check back daily!
This entry was posted in CakePHP, Internet, PHP. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>