PHP Class Tutorial: Learning the Basics

phpclasstutorialIf you have spent any time as a computer programmer, you are probably familiar with the term object oriented programming (OOP). If you are new to programming, OOP refers to creating classes of objects that can be manipulated as a whole. This means instead of modifying an entire program to change one behavior, you only need to modify the behavior once in a particular class to affect change on the entire program.

In PHP, classes can be used to group together a set of similar functions used within a bigger application. As previously mentioned, the advantage to this approach is that you can edit the functions of a particular class and make changes to the entire website or other PHP applications. As an added benefit, using classes in PHP gives you a more structured program that is easier to follow when future changes are required. If you want to learn more about object oriented programming, check out Programming for Non-Programmers.

Another benefit to an OOP approach is that most other programming languages are based on the same exact concept you will learn in this tutorial. This makes it extremely easy to adapt to a different syntax and create powerful programs in just about any programming language that relies on object oriented design.

The best way to think about programming with objects is to think about an object like a car. A car is capable of performing specific actions such as driving, stopping, and turning. The car also has properties at any given time. The car could be traveling at a certain speed or in a certain direction, the car is a specific color, and the car is a specific make and model. Classes in PHP (and any other programming languages for that matter) are very similar to a car. They also have certain actions they are capable of performing and have properties at any given time that affect the values of functions within the class.

Creating a PHP Class

Probably the best way to understand how to use classes in PHP effectively is to demonstrate how classes can be used instead of regular PHP functions. Look at the following example:

$sTime = gmdate(“d-m-Y H:i:s”);

print ‘The time is: ‘ . $sTime;

The above code assigns the current time and date to the variable $sTime and prints the string “the time is” with the variable value at the end. Please note that this is not a PHP class; instead, this is the normal way this function would be performed using PHP.

If the above code doesn’t make sense to you, consider taking the Killer Beginner’s PHP course before continuing.

Now, you can take this simple action and convert into a class file. The class file would look like this:

class Time {

 function GenerateCurrentTime(){

   $sTime = gmdate(“d-m-Y H:i:s”);

   return $sTime;

 }

}

By going through this simple example of a PHP class, you will better understand exactly how to construct your own classes of any size or complexity. The first line tells PHP that you are creating a new class and have decided to call it “Time.”

The next line declares a new function. Notice that the code for this new function is exactly the same as the code in the first example. The only difference is that this function only exists within the scope of the class. This means that the function cannot be accessed directly. You must call the class in your original PHP file to access this function.

The “return” line tells the function to return the current time and date to any other function that calls upon this class. Finally, the function closes and then the class closes.

Next, you have to go back to your original PHP file and modify it to take advantage of this PHP class. The code would look like this:

include (‘class.Time.php’);

$oTime = new Time;

$sTime = $oTime->GenerateCurrentTime();

print ‘The time is: ‘ . $sTime;

Note that the first line of this PHP file now includes the PHP class file you just created. If you do not include all of the class files you want to use, PHP has no way of knowing that you are using external class files for your script. If you are familiar with other programming languages, this is very similar to importing libraries in Java.

The next line (“$oTime = new Time”) creates an object of the Time class and stores it in the variable $oTime. Again, similar to other programming languages, you assign this variable using the basic formula of variable = new classname.

The next line assigns the variable $sTime to the results of the GenerateCurrentTime() function within the Time class. The result is that PHP assigns whatever is returned by the GenerateCurrentTime() function to the variable $sTime.

Finally, the last line prints out the results with the prefix string “The time is:”.

Learn PHP Programming from Scratch teaches you everything about using PHP; including a detailed section about using classes properly.

The Importance of PHP Classes

The above example may seem like a lot of extra work when everything can be defined in two lines of code using a standard PHP function. However, when you start looking at more complex websites with thousands of lines of code, it is very difficult to efficiently modify this code to change a single aspect.

Whether or not you choose to adopt an OOP design, The Basics of PHP for Web Development teaches you how to make sure you are building dynamic websites properly.

By doing the initial legwork and creating PHP classes for most of your primary functions, you can simply change this class file one time and the entire website or application will be modified automatically because the code refers to these class files instead of individual functions buried within the code.

If you are currently a PHP developer that is not using classes for any of your scripts, you will find your skill set becoming more and more antiquated. Today’s businesses expect you to use classes whenever possible to create code that can be picked up and easily understood by another developer in the future.

If you have no current programming experience and are in the process of learning PHP, take some time to understand how classes work. Practice the example in this tutorial and any others you can find. Your code will be more efficient and you will be well positioned for widespread adoption of OOP principles in the PHP community.