FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Architecture & Design
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
January 01, 2002
Object-Oriented Programming with PHP

WebReview.com: Object-Oriented Programming with PHP

Rank:3

PHP Resources

18,595 Links on Objects and Components: A massive OOP resource directory.

Object FAQ: Ugly frames, great site.

PHP.NET: News, references, support, and links of interest for all things PHP.

The high-paced development schedules of today's software has only punctuated the need for developers to possess efficient software strategies. One such strategy, known as object-oriented programming (OOP), has stepped to the forefront as the de facto standard for implementing strong software design. I'll examine the basic concepts of OOP and gain practical insight into how PHP can be used to develop OOP applications.

PHP, a popular server-side Web scripting language, is a logical tool for those wishing to keep up to pace with the application development field. Even novice programmers can build fantastically dynamic and flexible applications. Coupling PHP with an object-oriented strategy serves to optimize time and lets developers efficiently create cutting-edge applications.

Before delving into a series of examples illustrating just how this can be implemented, let's begin with a short introduction to OOP.

Practical OOP

OOP emphasizes the state, behavior, and interaction of data within an application. By focusing upon the data, the developer is better able to model the environment which he or she is attempting to implement. OOP results in more readable code, and greatly enhances the flexibility and management of a system.

One of the main advantages of OOP is that it enables programmers to build customized data modules, each having specific attributes and functionality. In OOP lingo, this customized data module is known as a class.

A class enables the programmer to model his application code upon real world concepts. Essentially, it can be thought of as an extended datatype which goes beyond the traditional mission of a datatype, defining not only attributes, but also functionality specific to that datatype. Much as a variable is assigned the attributes of a datatype, a new type of variable, known as an object, can be assigned the attributes and methods declared within a class.

To put it simply, a class can be thought of as a cookie-cutter. The class provides a template from which one or more objects can be made.

Building Classes in PHP

Defining classes in PHP is a rather straightforward task. Developers will find many of the programming concepts involved with class declaration to be familiar. Take a look at Listing 1.

For most of you, there are no real surprises in this code—just a few variable declarations and method (function) definitions. One oddity that you may have noticed is the reference to the variable $this. This variable is simply a reference to the object calling that method. Don't worry too much about it for now, as we'll touch upon this in further detail later on.

Building Objects in PHP

In PHP, as with most other object-oriented languages, an object is declared using the new keyword. This keyword specifies that a new object is to be created based upon the class reference following the new keyword. Let's illustrate this with a short example:


<?
// example 1 

// . . . Vehicle class declaration 

// declare a few objects

$motorcycle = new Vehicle;

$automobile = new Vehicle;

$plane = new Vehicle;

?>

Now that the objects have been created, we can begin interacting with them. This is where the advantages of OOP begin to shine through, particularly when it comes down to code readability and convenience.

Using Objects

A created object has its own copy of the attributes, and has use of all methods declared within that class. This is illustrated in the following example: Now that the objects have been created, we can begin modifying the attributes of each.


<?
// example 2 

$motorcycle->set_model("Harley");
$motorcycle->go(55);

$automobile->set_model("Corvette");
$automobile->go(95);

$model = "Cessna 172";

$plane->set_model($model);
$plane->go(165);

?>

Executing this code along with the above examples, the following would be output to the screen:


Vehicle model set to Harley.

The Harley is going 55 m.p.h..

Vehicle model set to Corvette.

The Corvette is going 95 m.p.h..

Vehicle model set to Cessna 172.

The Cessna 172 is going 165 m.p.h..

To summarize, we have now declared three new objects ( $motorcycle, $automobile, and $plane ). The attributes of these newly created objects were then modified using the methods found within the Vehicle class.

Perhaps it would be prudent to refer back to the purpose of the $this variable, first introduced within the Vehicle class declaration. Let's look to the method call within Example 2:

$automobile->set_model("Corvette");
The $automobile object is calling its method set_model. Therefore, $this refers to the object $automobile within the method body. It is simply a way to refer to the object calling the method.

Inheritance

As you can see from the simple examples above, the idea of basing your code on objects is certainly a potent one. However, this is only the basis for more powerful concepts of OOP. Not only can you develop these customized data modules, but you can also build relationships between these modules, leading to a highly structured and flexible programming environment.

One of the premises for building these relationships is known as inheritance.

Much like a child inherits characteristics from his or her parent, derived classes inherit the characteristics of the class in which they are derived, and are also known as child and parent classes, respectively.

So, suppose we want to further divide the Vehicle class into three subclasses: land, air, and water. All three of these subclasses should share the general attributes and functions of the Vehicle class, but also have attributes which are useful only within each specific subclass (wingspan for the air subclass, for example). To eliminate the unnecessary redeclaration of these attributes within each subclass, the subclass instead inherits the characteristics by a concept known as, conveniently enough, inheritance.

Another reason to use inheritance would be to further organize data structures into strictly meaningful entities. By this I mean that only the information that is relevant to that category will be included, and nothing else. After all, what use would wingspan have when implementing the water or land subclass? On the same note, num_wheels would have no place within the water subclass (unless perhaps we're discussing James Bond's boat).

Let's go ahead and create the land, air, and water subclasses.

Inheritance With PHP

The base class (Vehicle) will serve as the parent for the three child classes: land, air, and water. As stated, we want the three children to share the same attributes as possessed by the Vehicle class, in addition to owning their own characteristics specific to each individual purpose. This is known as "extending" the base class, as seen in Listing 2.

Putting the newly defined subclasses to use should clearly illustrate the power of inheritance:

<?

$my_jetski = new Water_vehicle;

$my_jetski->set_model("Sea-Doo");

$my_jetski->set_anchor(1);

?>
The resulting output:


Vehicle set to Sea-Doo.

Anchor is up.

This example not only illustrates the usage of the methods within the Water_vehicle subclass, but also those of the parent (Vehicle).

Another great feature of OOP is multiple inheritance, which is the idea of a subclass inheriting the attributes/methods from more than one parent class. Unfortunately, PHP does not yet support this feature.

All in the Family Tree

Now you've got some the basic concepts of object-oriented programming under your belt, illustrated with examples of PHP's OOP implementation syntax. Next time, we'll build upon this foundation, introducing several more of the more advanced notions of OOP design. In the meantime, take some time to experiment further with the OOP strategy. I guarantee it will be time well spent!


Jason is a Web/wireless application developer for Unstrung.

Enjoy these related articles by Jason Gilmore:

Our sister publication Web Techniques serves up these PHP offerings:

RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK