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

PHP and MySQL Web Development - P40 docx

5 334 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 112,87 KB

Nội dung

162 Chapter 6 Object-Oriented PHP When reading it, note that DisplayStyles(), DisplayHeader(),and DisplayFooter() need to display a large block of static HTML, with no PHP processing.Therefore, we have simply used an end PHP tag (?>), typed our HTML, and then re-entered PHP with an open PHP tag (<?php) while inside the functions. Tw o other operations are defined in this class.The operation DisplayButton() out- puts a single menu button. If the button is to point to the page we are on, we are dis- playing an inactive button instead, which looks slightly different, and does not link any- where.This keeps the page layout consistent and provides visitors with a visual location. The operation IsURLCurrentPage() determines if the URL for a button points to the current page. Lots of techniques can be used to discover this.We have used the string function strpos() to see if the URL given is contained in one of the server set vari- ables.The statement strpos( $GLOBALS['SCRIPT_NAME'], $url ) will either return a number if the string in $url is inside the global variable SCRIPT_NAME, or false if it is not. To use this page class, we need to include page.inc in a script and call Display(). The code in Listing 6.2 will create TLA Consulting’s home page and give output very similar to that we previously generated in Figure 5.2. The code in Listing 6.2 does the following: 1. Uses require to include the contents of page.inc, which contains the definition of the class Page. 2. Creates an instance of the class Page.The instance is called $homepage. 3. Calls the operation SetContent() within the object $homepage and passes some text and HTML tags to appear in the page. 4. Calls the operation Display() within the object $homepage to cause the page to be displayed in the visitor’s browser. Listing 6.2 home.php—This Home Page Uses the Page Class to Do Most of the Work Involved in Generating the Page <?php require ('page.inc'); $homepage = new Page(); $homepage -> SetContent('<p>Welcome to the home of TLA Consulting. Please take some time to get to know us.</p> <p>We specialize in serving your business needs and hope to hear from you soon.</p>' ); $homepage -> Display(); ?> 08 525x ch06 1/24/03 3:35 PM Page 162 163 Writing the Code for Your Class You can see in Listing 6.2 that we need to do very little work to generate new pages using this Page class. Using the class in this way means that all our pages need to be very similar. If we want some sections of the site to use a variant of the standard page, we could simply copy page.inc to a new file called page2.inc and make some changes.This would mean that every time we updated or fixed parts of page.inc,we would need to remember to make the same changes to page2.inc. A better course of action is to use inheritance to create a new class that inherits most of its functionality from Page,but overrides the parts that need to be different. For the TLA site, we want to require that the services page include a second naviga- tion bar. The script shown in Listing 6.3 does this by creating a new class called ServicesPage which inherits from Page.We provide a new array called $row2buttons that contains the buttons and links we want in the second row. Because we want this class to behave in mostly the same ways, we only override the part we want changed—the Display() operation. Listing 6.3 services.php—The Services Page Inherits from the Page Class but Overrides Display() to Alter the Output <?php require ('page.inc'); class ServicesPage extends Page { var $row2buttons = array( 'Re-engineering' => 'reengineering.php', 'Standards Compliance' => 'standards.php', 'Buzzword Compliance' => 'buzzword.php', 'Mission Statements' => 'mission.php' ); function Display() { echo "<html>\n<head>\n"; $this -> DisplayTitle(); $this -> DisplayKeywords(); $this -> DisplayStyles(); echo "</head>\n<body>\n"; $this -> DisplayHeader(); $this -> DisplayMenu($this->buttons); $this -> DisplayMenu($this->row2buttons); echo $this->content; $this -> DisplayFooter(); echo "</body>\n</html>\n"; } } 08 525x ch06 1/24/03 3:35 PM Page 163 164 Chapter 6 Object-Oriented PHP $services = new ServicesPage(); $content ='<p>At TLA Consulting, we offer a number of services. Perhaps the productivity of your employees would improve if we re-engineered your business. Maybe all your business needs is a fresh mission statement, or a new batch of buzzwords.</p>'; $services -> SetContent($content); $services -> Display(); ?> Our overriding Display() is very similar, but contains one extra line $this -> DisplayMenu($this->row2buttons); to call DisplayMenu() a second time and create a second menu bar. Outside the class definition, we create an instance of our ServicesPage class, set the val- ues for which we want non-default values and call Display(). As shown in Figure 6.2, we have a new variant of our standard page.The only new code we needed to write was for the parts that were different. Listing 6.3 Continued Figure 6.2 The services page is created using inheritance to reuse most of our standard page. 08 525x ch06 1/27/03 2:34 PM Page 164 165 Next to reuse most of our standard page. Creating pages via PHP classes has obvious advantages.With a class to do most of the work for us, we needed to do less work to create a new page.We can update all our pages at once by simply updating the class. Using inheritance, we can derive different versions of the class from our original without compromising the advantages. As with most things in life, these advantages do not come without cost. Creating pages from a script requires more computer processor effort than simply loading a static HTML page from disk and sending it to a browser. On a busy site this will be important, and you should make an effort to either use static HTML pages or cache the output of your scripts where possible to reduce the load on the server. Next The next section deals with MySQL.We’ll talk about how to create and populate a MySQL database, and then link what we’ve learned to PHP so that you can access your database from the Web. 08 525x ch06 1/24/03 3:35 PM Page 165 08 525x ch06 1/24/03 3:35 PM Page 166 . PHP processing.Therefore, we have simply used an end PHP tag (?>), typed our HTML, and then re-entered PHP with an open PHP tag (< ?php) while inside the functions. Tw o other operations. "<html> <head> "; $this -& gt; DisplayTitle(); $this -& gt; DisplayKeywords(); $this -& gt; DisplayStyles(); echo "</head> <body> "; $this -& gt; DisplayHeader(); $this -& gt; DisplayMenu($this->buttons); $this. section deals with MySQL. We’ll talk about how to create and populate a MySQL database, and then link what we’ve learned to PHP so that you can access your database from the Web. 08 525x ch06 1/24/03

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