Developer

Maximize PHP apps using OOP techniques

Contrary to popular belief, object-oriented programming (OOP) and PHP are actually very compatible. Find out how to harness your OOP expertise to build better PHP applications.


Most developers consider object-oriented programming (OOP) and PHP to be mutually exclusive, but PHP packs all the functionality needed for developers to aggressively use OOP techniques in their applications. To demonstrate this approach, let's use the classic example of a vehicle, written as a PHP class:
class Vehicle {
   // Stuff goes here!
}

Inside our new Vehicle class, methods (PHP functions) connect our application to Vehicle's characteristics. Each of these methods serves to describe Vehicle to the application, presumably so we can make use of it in some meaningful way. Take the following example (which obviously is nonfunctional):
class Vehicle {
   function getWheels() {
      // Logic to retrieve the number of wheels goes here.  
      return($wheels);
}
   function getDoors() {
      // Logic to retrieve the number of doors goes here.  
return($wheels);
}
}

Here we see a couple "getters." If all this looks and sounds familiar, that's because PHP's OOP functionality is pretty similar to that of C or Java. The real challenge is in using it elegantly, making full and efficient use of encapsulation.

To determine what you'll need to know about Vehicle to integrate it with your application, it may help to define the basic elements that make up a Vehicle. For example, Vehicles can be organized by the number of doors or number of wheels present.

If we want to know how many wheels our Vehicle has, we'll need a method that returns that information. In PHP, this could be done as shown in Listing A.

Now our Vehicle class contains a method called getWheels, which returns the number of wheels when passed a vehicle type by the application. How will we pass this information to our Vehicle class? To understand this better, let's demonstrate some usage, implementing our class and calling its getWheels method to determine the number of wheels on a vehicle of type car:
// Set the vehicle type
$vehicle = "car";

We've declared the type of vehicle we'd like to create to be a car. Since we're using PHP, this value can be dynamic, but for brevity we'll hard-code it.

To use our class, we first need to create an instance of the class in the application:
// Create a new instance of the car class
$myVehicle = &new Vehicle;

We've just created an instance of Vehicle, called myVehicle. This is a unique instance of our Vehicle class, and it contains all the data (methods and properties) contained in Vehicle.

Now it's time to find out how many wheels our car possesses:
// Get number of wheels by passing the vehicle type to getWheels
$numWheels = $myVehicle->getWheels($vehicle);

As you can see, we've called myVehicle'sgetWheels method and passed it the vehicle type. If all goes well, we'll get back the data we need:
// Print out the number of wheels
print "$vehicle has $numWheels wheels.";

If we want to get the number of doors, we need to write another method to check for the number of doors, as shown in Listing B.

Easy to program, easy to maintain
As you can see from this simple demonstration, object-oriented programming in PHP is not difficult. Using OOP and classes in the same manner as we did in our Vehicle class, it wouldn't be too much of a stretch to create an OOP shopping cart made of classes similar to our Vehicle class. Because it is object-oriented, the resulting code could be easy to read, maintain, and scale.

Listing C shows the larger version of the code for the car-sorting application we've discussed here. It should fully demonstrate the object-oriented capabilities of PHP.

 

Editor's Picks

Free Newsletters, In your Inbox