1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P52 pptx

5 215 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 179,33 KB

Nội dung

233 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 It’s not that amazing that the function can generate all that HTML code; the code is fairly predictable. What’s neat is the way the SuperHTML object knew what its title and headline should be. The phrase Basic Super Page is the string that initializes the SuperHTML object. The buildBottom() method is even easier than buildTop(), because it simply adds some boilerplate page-ending code: </body> </html> Writing Out the Page The buildTop() and buildBottom() directives feel a lot like function calls, because they are very similar to the functions you’ve already created and used many times. However, these functions are designed to work within the context of a par- ticular object. A function attached to an object is referred to as a method of the object. A cow object might have moo() and giveMilk() methods. The syntax for referring to methods in PHP is with the arrow syntax (->). There isn’t one key to indicate this operator. It is the combination of the dash and the greater- than symbol. Note that neither buildTop() nor buildBottom() actually write any code to the screen. Instead, they prepare the page as a long string property inside the object. SuperHTML has a method called getPage() that returns the actual HTML code for the page. The programmer can then save the code to a file, print it out, or what- ever. In this case, the following line simply prints out the results of the getPage() method: print $s->getPage(); Working with the Title Property It’s possible to designate a title when you create a SuperHTML object, but what if you want to change the title later? Objects can store code in methods, and they can also store data in properties. A property is like a variable attached to a par- ticular object. The SuperHTML object has a title property. The cow object might have a breed property and an age property. The Properties.php page featured in Figure 7.2 illustrates this feature. The Property.php program begins exactly like the Basic Super page you saw ear- lier. I even created the $s variable with the same initial value (Basic Super Page). HINT When I created the SuperHTML object, the title property was automatically set to Basic Super Page. It’s possible to directly change the title, like this: $s ->title = “new title”; As you see when you look at the SuperHTML code itself, this approach can cause some problems. It’s generally better to use special methods to get information to and from properties. Take a look at the following code for Property.php and you’ll see a better way to change a property value. <? include “SuperHTMLDef.php”; $s = new SuperHTML(“Basic Super Page”); $s->setTitle(“I changed this”); $s->buildTop(); print “The title is now “ . $s->getTitle(); $s->buildBottom(); print $s->getPage(); ?> The $s->setTitle() method allows me to add a new value to a property. The $s->getTitle() method gets a value from a property. These special methods are usually called access methods because they allow access to properties. I’ll explain more about access methods later in this chapter when you start building your own object. 234 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 FIGURE 7.2 I created this page with one title and then changed the title. 235 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 Adding Text and Tags with SuperHTML The SuperHTML object makes it easy to build a basic HTML framework, but you always need other kinds of tags. SuperHTML has some general methods for adding various kinds of tags and text to a document. Figure 7.3 illustrates a page using these features. One of the primary features of SuperHTML is the way it separates the creation of a Web page from its display. You want to be able to easily generate a page and then display it onscreen, write it to a file, or do whatever else you want with it. For that reason, you won’t simply print things out. Instead, you’ll keep adding stuff to the SuperHTML object and then print the whole thing out when you’re done. That means you need some mechanism for adding things to the page. The SuperHTML object contains more than 25 methods for adding various kinds of objects to the document. (Don’t panic. Most of them are really very simple.) Two methods in par- ticular are extremely useful. Look at the code for AddText.php and see what I mean. FIGURE 7.3 This page includes some text and HTML tags. IN THE REAL WORLD If you’ve programmed in languages like Visual Basic, C#, or Java, you might argue that you have directly accessed properties without using these access methods. The truth is, access methods in these languages are usually behind the scenes. When you assign a value to an object property, the appropriate access method is automatically implemented. 236 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 <? include “SuperHTMLDef.php”; $s = new SuperHTML(“Adding Text and Tags”); $s->buildTop(); $s->addText(“This is ordinary text added to the document”); $s->addText(“<div>You can also add HTML code <hr> like the HR above</div>”); $s->h3(“Use h1-h6 methods to add headings”); $s->tag(“i”, “this line is italicized”); $s->buildBottom(); print $s->getPage(); ?> The addText() method expects a string as its only parameter. It then adds that text to the document in memory. As you can see, the text can even contain HTML data. You can also pass multi-line strings or text with interpolated variables. The addText() method is really the only method you need in order to build the page in memory. However, the point of the SuperHTML object is to make page devel- opment faster and easier. I actually use the addText() method when I need to add actual text to a page or when I need a tag I haven’t yet implemented in SuperHTML. Look at the following line: $s->h3(“Use h1-h6 methods to add headings”); This code accepts a string as a parameter, then surrounds the text with <h3></h3> tags and writes it to the document in memory. Of course, there are similar methods for h1 through h6. You could expect similar methods for all the basic HTML tags. I didn’t create shortcuts for all the HTML tags, for two reasons. One reason is once you see the mechanism for creating a new tag method, you can modify SuperHTML very easily to have methods for all your favorite tags. The other reason I didn’t make shortcuts for all the tags is the very special method described in the fol- lowing line: $s->tag(“i”, “this line is italicized”); The tag() method is a workhorse. It expects two parameters. The first is the tag you wish to implement (without the angle braces). In this case I want to italicize, so I’m implementing the i tag. The second parameter is the text you want sent to the document. After this function is completed, the document has the following text added to the end: <i>this line is italicized</i> If you look at the HTML source code for AddText.php, you see that’s exactly what happened. The great thing about the tag() method is its flexibility. It can surround any text with any tag. Creating Lists the SuperHTML Way Even if you only use the addText() and tag() methods, you can create some really nice, flexible Web pages. However, this object’s real power comes with some spe- cialized methods that solve specific display problems. When you think about it, a lot of PHP constructs have natural companions in the HTML world. For exam- ple, if you have an array of data in PHP, you frequently want to display it in some form of HTML list. Figure 7.4 demonstrates a page with a number of lists, all auto- matically generated from arrays. You’ve probably already written code to generate an HTML list from an array. Although it’s not difficult, it can be tedious. It’d be great if you could just hand off that functionality and not worry about it when you’ve got other problems to solve. The SuperHTML object has exactly that capability. The code list.php illus- trates a number of ways to do this. 237 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.4 These HTML lists were created automatically from arrays. . have a breed property and an age property. The Properties .php page featured in Figure 7.2 illustrates this feature. The Property .php program begins exactly like the Basic Super page you saw. properties. Take a look at the following code for Property .php and you’ll see a better way to change a property value. <? include “SuperHTMLDef .php ; $s = new SuperHTML(“Basic Super Page”); $s->setTitle(“I. problems. When you think about it, a lot of PHP constructs have natural companions in the HTML world. For exam- ple, if you have an array of data in PHP, you frequently want to display it in some form

Ngày đăng: 07/07/2014, 02:20

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN