This page intentionally left blank W r i t i n g P r o g r a m s w i t h O b j e c t s 7 CHAPTER O bject-oriented programming (sometimes abbreviated as OOP ) is an important development in programming languages. Some languages such as Java are entirely object oriented and require an intimate understanding of the object-oriented paradigm. PHP, in keeping with its easygoing nature, supports a form of object-oriented programming, but does not require it. In this chapter you learn what objects are and how to build them in PHP. Specifically, you learn how to: • Use a custom class to build enhanced Web pages • Design a basic class of your own • Build an instance of a class • Leverage inheritance, encapsulation, and polymorphism • Create properties and methods in your classes 230 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r Introducing the SuperHTML Object Back when I used to program in Perl (before PHP existed), I really liked a feature called cgi.pm. This was a module that simplified server-based programming. One of my favorite things about the module was the way it allowed you to quickly build Web pages with function calls. Once I understood how to use cgi.pm, I was creating Web pages with unbelievable speed and ease. PHP doesn’t have this fea- ture built in, so I decided to make a similar object myself using the object-oriented paradigm. Throughout this chapter you see the object being used, then you’ll learn how to build it and modify it for your own purposes. As you look at the SuperHTML object, you should think of it at two levels: • First, ignore the inner workings of the object and learn how to use it. There’s a lot to this class, and it has a lot of potential to improve your PHP programming. • Second, examine how it was built. This comes after you’ve played with the object for a while. There’s no doubt you’ll have some ideas on how to improve it. Once you understand how objects work in PHP, you’ll be able to build even more-powerful objects on your own. Building a Simple Document with SuperHTML The SuperHTML object is a special type of entity that allows you to automatically create and modify a Web page. It can be tricky to understand what an object is, so I’ll start by just showing you how it works. Take a look at Figure 7.1 to see a simple HTML page. What makes this document interesting is the way it was built. The source code for BasicSuper.php is different from any other code you’ve seen so far. <? include “SuperHTMLDef.php”; $s = new SuperHTML(“Basic Super Page”); $s->buildTop(); $s->buildBottom(); print $s->getPage(); ?> Including a File The first thing this code does is import another file. The include statement retrieves data from another file and interprets it as HTML code. In this case, I’m including a file called SuperHTMLDef.php. That file contains the definition for the SuperHTML object. One of the joys of OOP is using an object without understanding exactly how it works. Rest assured you’ll see the code in the file soon enough. For now be sure you understand how to access code in another program. If you are having trouble with the include command, check the value of open_basedir in php.ini. (The easiest way to do this is to run a program with the phpInfo() command in it.) The open_basedir variable is set to null by default, which means PHP can load files from anywhere in the server’s file system, but your administrator may have this access limited. You may need to reset this value on the server or bribe your server administrator to get this value changed. They (administrators, not servers) generally respond well to Twinkies. Creating an Instance of the SuperHTML Object The next line creates a new variable called $s: $s = new SuperHTML(“Basic Super Page”); TRAP 231 C h a p t e r 7 W r i t i n g P r o g r a m s w i t h O b j e c t s FIGURE 7.1 The basic SuperHTML page looks like a normal HTML page to the user. This variable contains an object. Objects are complex variable types that can con- tain both variables and code. The SuperHTML object is a custom object I invented that describes a fancy kind of Web page. Since it’s an object, it has certain charac- teristics, called properties, and it has certain behaviors, called methods. By invok- ing the object’s methods, you can do some really neat things without a lot of work. Most of the properties are hidden in this particular example, but they’re still there. Notice that when I created $s, I indicated a name for the page: Basic Super Page. That name will be used to form the title of the page as well as a caption. No doubt you recall my advice to use descriptive names for things. You might be composing an angry e-mail to me now about the variable called $s: That’s clearly not a descriptive name. However, I’m following a convention that’s reasonably common among object-oriented programmers. When I have one particular object that I’m going to use a lot, I give it a one-character name to keep the typing simple. If that bothers you, the global search and replace feature of your favorite text editor is a reasonable option. Building the Web Page The Web page shown in Figure 7.1 has all the normal accoutrements, like <head> and <body> tags, a <title>, and even a headline in <h1> format. However, you can see that the code doesn’t directly include any of these elements. Instead, it has two lines of PHP that both invoke the $s object. $s->buildTop(); $s->buildBottom(); Both buildTop() and buildBottom() are considered methods, which are simply func- tions associated with a particular object type. In effect, because $s is a SuperHTML object, it knows how to build the top and bottom of the Web page. The buildTop() method generates the following HTML code automatically: <!doctype html public “-//W3C//DTD HTML 4.0 //EN”> <html> <head> <title>Basic Super Page</title> </head> <body> <center> <h1>Basic Super Page</h1> </center> TRICK 232 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r . of open_basedir in php. ini. (The easiest way to do this is to run a program with the phpInfo() command in it.) The open_basedir variable is set to null by default, which means PHP can load files. the way it was built. The source code for BasicSuper .php is different from any other code you’ve seen so far. <? include “SuperHTMLDef .php ; $s = new SuperHTML(“Basic Super Page”); $s->buildTop(); $s->buildBottom(); print. paradigm. PHP, in keeping with its easygoing nature, supports a form of object-oriented programming, but does not require it. In this chapter you learn what objects are and how to build them in PHP.