Really Cool Classes in PHP

Web Design & Marketing Blog


Really Cool Classes in PHP

PHP is super easy. This is a wonderful thing, and don't let anyone tell you otherwise. PHP lets you skip some of the more complicated features of other coding languages so that you can easily write small shell scripts and make simple, easy web pages with almost no training. However, the real wonder in PHP comes from utilizing features like object oriented programming.

Object Oriented Programming

Chances are you've heard this buzz word, even if you don't remember hearing it. It's one of the most important revolutions in programming technique, and it makes programming magnitudes more awesome. I myself disdain the plague of prolix buzz words and ridiculously impossible to remember acronyms so rampant in computer science, and so I'm going to urge anyone to avoid any rote memorization of of the endless lists of features that define object oriented programming. There is so much that you can do with it that I see no point in trying to make an arbitrary list of what makes something kosher 'object oriented'. The important concepts are the class and the object, and all the cool things you can do with them is just gravy.

The Class

In a large program, it's best to put any similar PHP code into classes. In fact, planning what classes you want should come before you even have any code to put into them. Say you're making a book store. You'll have to deal with different authors, books, genres, etc. You'll want to make a class for each one in a file that has the exact same name as the class and the extension '.php'. There are several best practices I can bore you with here, but instead just take a look at this:

<?php
/**
* This class is a collection of variables and functions that all have something
* to do with authors.
*
* We make a comment that looks just like this before every class
* and function because a lot of programs will look for and use comments
* in this format and we want people to know what our code is for and how
* to use it.
*
* This class is 'extending' another class named Person, meaning it gets all
* of the functions and variables of that class so that we don't have to write
* variables for things like names and phone numbers again.
*/

class Author extends Person {
// This is where we declare any variables we want every function in the class to be able to use. Like a database connection so that we don't have to connect over and over again.
// We can make them private so another file calling this class can't use it, protected so a class we make extend this one can use it, or public if we just don't care what happens to it.
private $con;

/**
* This runs every time we make an object of this class.
* As just one example of the many useful purposes of a
* constructor, this is a good place to either pass an existing
* connection to a database, or make a new one.
*/

function __construct($con = FALSE) {
if($con == FALSE) {
$this->con = mysql_connect('localhost', 'user', 'password');
} else {
$this->con = $con;
}
}

/**
* 'Static' means we use this function without making an object,
* otherwise we have to make an object first (I'll show you later).
*
* Functions can also be public, protected, or private like variables.
*/

public static function getAuthors($genre) {
...
}

/**
* Sometimes the functions we get from a class we're extending
* from need more to them, so we can just declare them again.
*
* Pretend the class 'Person' has this same method we could use
* in 'Author' because we're extending it, but we want the class
* Author to do something different when we get the object name.
*/
public function getPersonName() {
...
}

/**
* Every time an object is removed such as when a script is done
* this function will be called.
* One thing you can do here is delete any files the class might make
* that won't get destroying if something doesn't get called.
*/
function __destruct() {
...
}
}

?>

Using Classes: Objects

Now we can call all of our static functions that are public simply by typing $authors = Author::getAuthors('action'), but what we really want to do is make objects. In fact, one of the best reasons to make static functions is to return objects like we can pretend the example above would do. This is how we make and use objects:

<?php

require_once('pathToClasses/Author.php');

$author = new Author();
$author->setName('Guy');

$con = mysql_connect('localhost', 'user', 'password');
$anotherAuthor = new Author($con);

$anotherAuthor->setName('Bob');

print $anotherAuthor->getName() . ' is a different author object than ' . $author->getName();

?>

When to Use Classes

Almost always use classes. And when I say almost always, I pretty much mean always. In most web frameworks (I mean MVC frameworks) the web address itself goes to a file that makes an object that will simply call a function that includes the HTML for the header, content, and footer. This is our 'controller'. Now it's true that even in some of the most serious object oriented programs in PHP we want code that isn't in an object. In the files with our HTML, or the 'view' of our framework, we may want to do some small, simple things, or maybe use objects that we've created in the controller, but that needs to be kept to a minimum. All of the hard work should be in functions in the controller class or called in the controller class by making objects of classes that talk to the database, or the classes that make up the 'model' of our framework. It's things like this that allow us to use awesome programs for our database like ORM's.

What's so Great About It?

If you make some of your code into a class, you can use reuse any of its functions in any PHP program. Other people can even use it. If you put all of the code that deals with the database in classes, you can easily change how a program talks to the database in case there's a change in the database or how the database is connected to. If you write a bunch of great base classes, you can extend more specific classes from them in programs you'll be writing for years. This is what allows for cool new stuff like content management, shopping carts, and slideshows to get put together into an all-solutions website service like Lexy Sites. Putting everything into a class now is like investing. The sooner you start doing it, the more time you'll save. The sooner you start doing it, the cooler you can make classes for future use, and you'll save even more time from that. In short, not putting your code into classes is a suckers game.

OUR LATEST TWEETS

Testimonials

VivaNet2.0 constructed website, myUSAi.org according to our exact technical specifications and with our required timeframe and budget. We liked VivaNet2.0...

Steve Wilson, myUSAi.org

GET SOCIAL
  • Facebook
  • Twitter
  • Linkedin
  • Feed