Jump to content

Class? Objects?


Recommended Posts

Have you ever tried to do a google search for information on "class" or "php class", "php classes" and the like?

It's completely useless unless you're looking to find a teacher, and a class they are doing.

It took me a long time to figure out what a php class is and what it does. I'm not intending to write a guide on how classes work, but merely to introduce some ideas of what a class is in php for those, who like me, may have googled "php class" and have come up empty handed.

The first thing you should know, is the names of two types of programming styles.


  • Procedural Programming
  • Object Oriented Programming


Procedural Programming

For all of you that are familiar with mccodes, procedural programming is what you are seeing, with the one exception being the the Header class in versions 1 and 2, and the Database class in version 2.

Procedural is simply doing things in a linear fashion. You do one thing, then another, and then another. The code follows a straight progression from start to end. There's nothing wrong with this style of coding.


Object Oriented Programming

OOP, short for object oriented programming is where classes and objects come into play.

What is a class?

A class is a definition. It's that simple. Making a new class is defining how something ought to work. If I am going to make a database class, I'm defining how connecting to, get data from, and submitting data to a database should work. Creating the class does nothing to the database by itself. To use a comparison from the author Larry Ullman, a class is like a blueprint for a house. That blueprint is not in and of itself a house, and that blueprint doesn't build a house. It tells you how to build it.

What is an object?

An object is what you have when you use a class. To illustrate, let's say we have defined a class for handling database connections. We might call that class "Database". In order to use that class, we'll use the code below.


$db = new Database();


What we have done here, is used the class called "Database" and created an object called $db.

There's a lot more I could get into here, but the point of this writing is to simply introduce the concept of classes and what the basic nomenclature is.

You know know that a class is a definition of how something works. An object is how you put that class definition into use.

From here, numerous books have been written about classes and objects. Hopefully if classes and objects confused you before, as to what they are in their most basic form, this sets some boundary definitions for each.

**Random Thoughts**

Just an FYI, the Header class in mccodes is a terrible implementation of a class. Why is that? What is the class designed to do? It's designed to wrap a php page in an html header and footer. There's nothing wrong with wrapping your pages in a header and footer template, but using a class to do so is not the way to go.

The first thing I ask myself is what do we gain by using a class for the header? Nothing. Since we have gained nothing, then why did we do it? Is it just because we could? That's not a good reason to use OOP. The functions in the header class could just as easily have been functions in the global_func.php file and nothing about mccodes would work differently.

The Database class in mccodes vs 2 is a different case. It makes perfect sense to use a database class to simplify using a database with php. mccodes Database class suffers from some lack of functionality, which is based in part on PHP 4's deficient OOP implementation. However, beginning with PHP 5, OOP in PHP is far far better does make things work more efficiently and makes it easier to code.

Now there's no reason to rip out the header class and go the procedural route since the header class does work. But keep it in mind when you are making a class of your own the differences between the header and the database class, and you can use them as base examples of a bad and a good implementation of a class.

Link to comment
Share on other sites

  • 1 month later...

Re: Class? Objects?

Just a quick info: OOP (object oriented programming) is not always the best choice as on the other side procedural programming is not neither always the best choice. If you start a project from scratch, before choosing one or the other solution, read this, and think about it:

http://devzone.zend.com/article/1236-Th ... ing-in-PHP

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...