Occasionally in this book, I've mentioned "object-oriented programming" in reference to a more complex type of programming, beyond the procedural scripts on which this book is based. An object is sort of a theoretical box of things— variables, functions, and so forth—that exists in a templated structure called a class. Although it's easy to visualize a simple variable, such as $color, with a value of red, or an array called $rainbow with three or four elements inside it, some people have a difficult time visualizing objects.
For now, try to think of an object like a little box with inputs and outputs on either side. The input mechanisms are called methods, and methods have properties. Throughout this section, you'll take a look at how classes, methods, and properties all work together to produce various outputs. This will give you a good picture of what object-oriented programming is all about, should you want to go that route when building your applications. To learn plenty more on the topic, check the PHP manual chapter called "Classes and Objects" at http://www.php.net/manual/en/language.oop.php.
An object has a structure, called a class. In a class, a set of characteristics is defined. For example, say that you have created a house class. In the house class, you might have architectural type, square footage, and color characteristics. Each house object uses all of the characteristics, but they are initialized to different values, such as ranch, 1500, and white aluminum siding, or condominium, 786, and tan stucco.
Because classes are so tightly structured but self-contained and independent of one another, they can be reused from one application to another. For example, suppose you write some text-formatting classes on one project and decide you can use that class in another project. Because a class is just a set of characteristics, you can pick up the code and use it in the second project, reaching into it with methods specific to the second application, but using the inner workings of the existing code to achieve new results.
Creating an object is quite simple; you simply declare it to be in existence:
class myClass {
//code will go here
}
Now that you have a class, you can create a new instance of an object:
$object1 = new myClass();
The following code snippet shows you that your object exists:
<?php
class myClass {
//code will go here
}
$object1 = new myClass();
echo "\$object1 is an ".gettype($object1);
?>
If you save this code as objtest.php, place it in your document root, and access it with your web browser, you will see the following on your screen:
$object1 is an object
Next, you learn about object properties and methods.
The variables declared inside an object are called properties. It is standard practice to declare your variables at the top of the class. These properties can be values, arrays, or even other objects. The following snippet uses simple scalar variables inside the class, prefaced with the var keyword:
class myHouse {
var $type = "condo";
var $sqfootage = "786";
var $color = "tan stucco";
}
Now, when you create a myHouse object, it will always have these three properties, which you can reference. The following shows how this works:
<?
class myHouse {
var $type = "condo";
var $sqfootage = "786";
var $color = "tan stucco";
}
$house = new myHouse();
echo "I live in a ".$house -> color." ".$house -> sqfootage."
square foot ".$house -> type;
?>
If you save this code as myHouse.php, place it in your document root, and access it with your web browser, you will see the following on your screen:
I live in a tan stucco 786 square foot condo
Methods add functionality to your objects; instead of simply containing properties, the objects will actually do something useful. For example, the following class outputs a string:
<?
class anotherClass {
function sayHello() {
echo "HELLO!";
}
}
$object1 = new anotherClass();
$object1 -> sayHello();
?>
If you save a file containing this code in your document root and access it with your web browser, you will see the following on your screen:
HELLO!
So, a method looks and acts like a normal function but is defined within the framework of a class. The -> operator is used to call the object method in the context of your script. Had there been any variables stored in the object, the method would have been capable of accessing them for its own purposes. For example:
<?
class anotherClass {
var $name = "Joe";
function sayHello() {
echo "HELLO! My name is ".$this->name;
}
}
$object1 = new anotherClass();
$object1 -> sayHello();
?>
If you save this code and place it in your document root, and then access it with your web browser, you will see the following on your screen:
HELLO! My name is Joe
The special variable $this is used to refer to the currently instantiated object. Anytime an object refers to itself, you must use the $this variable. Using the $this variable in conjunction with the -> operator enables you to access any property or method in a class, within the class itself.
A constructor is simply a function that lives within a class and, given the same name as the class, is automatically called when a new instance of the class is created using new classname. Using constructors enables you to provide arguments to your class, to be processed immediately when it is first called. You will see constructors in action in the next section.
Having learned the very basics of objects, properties, and methods, you can start to look at object inheritance. Inheritance with regard to classes is just what it sounds like: One class inherits the functionality from its parent class. An example is shown here:
<?
class myClass {
var $name = "Joe";
function myClass($n) {
$this->name = $n;
}
function sayHello() {
echo "HELLO! My name is ".$this->name;
}
}
class childClass extends myClass {
//code goes here
}
$object1 = new childClass("Child of Joe");
$object1 -> sayHello();
?>
If you save this code, place it in your document root, and access it with your web browser, you will see the following on your screen:
HELLO! My name is Child of Joe
These lines make up the constructor:
function myClass($n) {
$this->name = $n;
}
This function is named the same as the class in which it is contained: myClass. You then see that the childClass is defined, but it contains no code. In this example, it's meant to demonstrate only inheritance from the parent class. The inheritance occurs through the use of the extends clause, as in
class childClass extends myClass {
The second class inherits the elements of the first class because this clause is used. Like most elements of object-oriented programming, inheritance is useful when attempting to make your code flexible. Suppose you created a text-formatting class that organized and stored data, formatted it in HTML, and output the result to a browser—your own personal masterpiece. Now suppose you had a client who wanted to use that concept, but instead of formatting the content into HTML and sending it to a browser, he wanted to format it for plain text and save it to a text file. No problem—you just add a few methods and properties, and away you go. Finally, the client comes back and says that he really wants the data to be formatted and sent as an e-mail—and then, what the heck, why not create XML-formatted files as well?
If you separate the compilation and storage classes from the formatting classes— one for each of the various delivery methods (HTML, text, e-mail, and XML)—you essentially have a parent-child relationship. Consider the parent class to be the one that holds the compilation and storage methods. The formatting classes are the children—they inherit the information from the parent and output the result based on their own functionality.
In no way has this brief appendix covered all the aspects of object-oriented programming—universities teach entire series of courses devoted to this topic. However, you did learn how to create classes and instantiate objects from them, how to create and access the properties and methods of a class, how to build new classes, and how to inherit features from parent classes. You should now be able to pick up a book on object-oriented programming and not feel completely lost.